开发者

How to pass session information to a subdirectory in php

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_sta开发者_JAVA百科rt();
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"); 


session_is_registered() is deprecated and shouldn't be used.

The equivalent "modern" code would be

session_start();
if (!isset($_SESSION[myusername])) {
  ...
}

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 used define() 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜