如何将会话信息传递到php中的子目录
我正在用 PHP 创建一个用户可以登录的简单网站。
根目录中的所有页面都工作正常,但是当我尝试在子目录中的网页上启动会话时,它不会读取它
这是我在子目录中网页开头的内容,正如我所说对于根目录中的每个页面都可以正常工作 -
session_start();
if(!session_is_registered(myusername)){
header("location: http://www.four-corners.org.uk/welcome.php");
有几个人似乎有同样的问题,但我还没有找到解决方案
编辑 -
下面是我用来定义“myusername”的代码和在会话中注册它
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=md5($_POST['mypassword']);
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
I'm creating a simple website in PHP that users can log into.
All pages in the root directory work fine, but when I try and start a session on a web page in a subdirectory it doesn't read it
This is what I have at the start of the web pages in the subdirectory, which as I say works fine for every page in the root directory -
session_start();
if(!session_is_registered(myusername)){
header("location: http://www.four-corners.org.uk/welcome.php");
several people seem to have the same problem but I've not found a solution yet
Edit -
Below is the code I've used to define 'myusername' and register it in the session
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=md5($_POST['mypassword']);
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
session_is_registered()
已弃用,不应使用。等效的“现代”代码是请
注意,我已经完全复制了您的“myusername”。如果前面没有
$
符号,则将其视为常量。如果您没有使用define()
设置该常量,它将计算为 null 并在进入 session_is_registered 之前强制转换为空字符串。除非您的会话具有以空字符串作为键的值,否则您每次都会得到重定向。session_is_registered()
is deprecated and shouldn't be used.The equivalent "modern" code would be
Note that I've duplicated your 'myusername' exactly. Without a
$
sign in front of it, it's treated as a constant. If you've not useddefine()
to set that constant, it'll evaluate to a null and get cast to an empty string before going into session_is_registered. Unless your session has a value with an empty string as a key, you'll get redirect every time.