我用的系统是CentOS,网站根目录是/var/www/html,根目录放有head.inc.php,当我在index.php里require('head.inc.php')时一切正常,require('./head.inc.php')时也正常,就是require('/head.inc.php')时候发现failed to open stream: No such file or directory的错误
前面的斜杠代表的是根目录,难道我设置有误?Apache的DocumentRoot和Directory都设置成了/var/www/html,www,html和文件都设置成了rwxr-xr-x,还有其他需要设置的吗?谢谢!
回复讨论(解决方案)
require('/head.inc.php') 指 /var/www/html/head.inc.php 也就是php认为head.inc.php是一个在html路径下的目录,而不是一个php文件。
或者你可以这样写:require(dirname(__FILE__).'/head.inc.php')
require 使用的是文件系统的路径而不是 web 的路径
谢谢changjay和xuzuning,但是dirname(__FILE__)获取的是当前文件的路径吧?我要获取根目录,例如有个子目录文件/var/www/html/report/index.php同样也要reqire根目录下的head.inc.php,如果用dirname(__FILE__)的话,就变成了require '/var/www/html/report/head.inc.php'
版主提到的是文件系统的路径,我想可能是这个原因,我试下改成/var/www/html/head.inc.php
是不是你这个网站节点的根目录没配置好!
版主,我试着改成了文件系统目录,可以了
zmzkkk,我已经把Apache的DocumentRoot和Directory都设置成了/var/www/html,还有其他要修改的吗?
网上说用/就表示根目录,好像include也是按文件系统目录的,这个问题google了半天也很少人提及
大家还有什么更好的解决办法?晚上结贴
是的 / 表示根目录
既然你的在做网站,那么你就不得不面对两个系统,也就是有两个根
网站的文件存放在操作系统的文件系统中, /var/www/html, /是文件系统的根
浏览器访问的是 http://localhost /index.php, /是 web 系统的根
web 系统的根是由你在 web 服务器中指定的,如
DocumentRoot = /var/www/html
其实 php 已经给出了解决方案:include_path
谢谢changjay和xuzuning,但是dirname(__FILE__)获取的是当前文件的路径吧?我要获取根目录,例如有个子目录文件/var/www/html/report/index.php同样也要reqire根目录下的head.inc.php,如果用dirname(__FILE__)的话,就变成了require '/var/www/html/report/head.inc.php'
版主提到的是文件系统的路径,我想可能是这个原因,我试下改成/var/www/html/head.inc.php
如果有配置文件的话,可以在配置文件里先把网站根目录赋值给一个变量,然后在需要的地方使用。
谢谢版主,但是我改了/etc/php.ini的include_path=".:/var/www/html",还是会出现:
Fatal error: require() [function.require]: Failed opening required '/head.inc.php' (include_path='.:/var/www/html') in xxx
我改后重启了linux,确认var,www,html以及html下面所有文件都是755,关闭了SeLinux(getenforce=permissive),但是还是错误。改成了你说的require('/var/www/html/head.inc.php')就可以,我再查查
Novolee,我也想过一个全局变量,但是绝对路径还是会泄露到php代码里
发现还有个变量$_SERVER['DOCUMENT_ROOT'],我用这个变量替代/又可以了
就是直接用/为什么还不可以
发现还有个变量$_SERVER['DOCUMENT_ROOT'],我用这个变量替代/又可以了
就是直接用/为什么还不可以
接受??及大?的做法: 使用你最早的方式 ./ 除非你的是打算在命令行下?行的?本那麽就??路??全.
发现还有个变量$_SERVER['DOCUMENT_ROOT'],我用这个变量替代/又可以了
就是直接用/为什么还不可以
前面不是有人回答了吗?直接用/,表示的是操作系统文件系统的根目录,而不是站点的根目录。
PhpNewnew,我的网页有好几层,但是header.inc.php,css样式表全部放在根目录里,所以直接用根目录加载这些会方便很多。./只能读取到当前目录
Novolee,是,但是php里就必须写成/var/www/html/head.inc.php了,有点担心这样的路径会不会不小心显示到网页里,所以还是用'DOCUMENT_ROOT了
谢谢大家,结贴

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
