PHP会话变量在新页面上消失

时间:2012-03-14 20:13:00

标签: php session cookies

我一直试图弄清楚我在使用PHP和Session Cookie时遇到的这个问题。出于某种原因,当我转到同一服务器中的新页面时,sesion变量不会填充,但session_id()是。如果我回去重新登录,它会起作用吗?我在这里尝试了几个链接并用Google搜索,但似乎没有任何效果。

class SessionRepository
{

public $errors;

public function StartSession()
{
session_start();
}

public function SetUserSession($userName,$fullName)
{
        // Start session
        $this->StartSession;

        // Make sure there isn't something already set
        /*if (isset($_SESSION['userName']))
            unset($_SESSION['userName']);
        */
        // Make sure there isn't something already set
        /*if (isset($_SESSION['fullName']))
            unset($_SESSION['fullName']);
*/
        $_SESSION['userName'] = $userName;
        $_SESSION['fullName'] = $fullName;


}

public function CheckLogin($userName)
{
        $this->StartSession;
        if(empty($_SESSION[$userName]))
        {
            return false;
        }
        return true;
}

public function DestroyUserSession()
{
    $this->StartSession;

    session_destroy();
}

}






include("useritemrepository.php");
include('UserItem.php');
include('sessionrepository.php');
include('login.php');

if(!isset($_POST['submitcreds']))
{
    LoginPage($loginerror);
}
else
{


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
        $userName = $_POST["userName"];
        $password = md5($_POST["password"]);

        $session = new SessionRepository();
        $userRepository = new UserItemRepository();

        $loggedin = $userRepository->LoginUser($userName,$password);

        if($userRepository->errors !="")
                    echo $userRepository->errors;

        if ($loggedin->UserName =="")
        {
            $loginerror = "Invald Credentials, Please try again";

            LoginPage($loginerror);
        }
        else
        {
        $fullName = $loggedin->FirstName . " " . $loggedin->LastName;

        // Build Session 
        $session->SetUserSession($userName,$fullName);
        echo "Session:" . $_SESSION["fullName"] ." ". $_SESSION["userName"];
        LoggedIn();
        exit();
        }

    }
    }

然后是登录页面(login.php)

session_start();
function LoginPage($loginerror)
{

        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>
        Cold Calling Center
        </title>
        <link rel='stylesheet' href='site.css'/>
        </head>

        <body>

        <br/>
        <br/>
        <br/>
        <div id="wrapper" class="wrapper" width="600px" height="600px">
        <form name="login" action="<? $_SERVER["PHP_SELF"];?>" method="post">
        <span id="login-header" class="login-header"><? echo $loginerror;?></span>
        <br/>
        <br/>
        <span id="header">Welcome to Cold Calling Central</span>

        <table id="login-table">
        <th id="table-header">Login Form</th>
        <tr id="login-row">
        <td id="login-cell">UserName:</td>
        <td id="login-cell"><input type="text" name="userName"/></td>
        </tr>

        <tr id="login-row">
        <td id="login-cell">Password:</td>
        <td id="login-cell"><input type="password" name="password"/></td>
        </tr>
        <tr id="login-row">
        <td id="login-cell"><input type="submit" name="submitcreds" value="Login"/></td>

        </tr>
        <tr id="login-table">
        <td id="login-row"><a href="forgotPassword.php">Forgot Password</a></td>
        </tr>
        <tr id="login-table">
        <td id="login-row"><a href="register.php">Not Registered?</a></td>
        </tr>
        </table>
        <input type="hidden" name="PHP_AUTH_USER"/>

        </form>
        </div>

        </body>

        </html>
 }

function RedirectToPage()
{
$url ="loggedin.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<META HTTP-EQUIV="Refresh"
      CONTENT="1; URL=<? 
      /*Redirect user to their page upon update */
      echo $url;?>">
<title>Thank you for logging in</title>
<link rel="stylesheet" type="text/css" media="screen" href="briefing.css"/>
</head>

<body>
<div id="shadow">
<div id="wrapper">
<div id="branding"></div> 
<div id="content">

<h2></h2>


<table class="briefing" width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="2"><h4>Thank you for logging in <? echo $_SESSION['fullName'];?>.. you are now being redirected to your page!</h4></td>
        <td>&nbsp;</td>
    </tr>



</table>


</div>


</div>
</div>
</body>
</html>
<?
}

function LoggedIn()
{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="HTML Tclassy for Linux (vers 6 November 2007), see www.w3.org">
<title>Registration</title>
<link rel='stylesheet' href='site.css' type="text/css">
</head>
<body>
    <div id="wrapper" class="wrapper">
        <span id="login-header" class="login-header"><? echo "Welcome: " . $_SESSION['fullName']; ?></span>

                <ul id="menu-List" class="menu-list">
                <li class="list-item"><a class="menu-link" href="loggedin.php">Home</a></li>
                <li class="list-item"><a class="menu-link" href="test.php">Home</a></li>
                <li class="list-item"><a class="menu-link" href="index.php">Home</a></li>
                <li class="list-item"><a class="menu-link" href="index.php">Home</a></li>
                </ul>
    </div>

</body>
</html>
}

以下是目标网页中其中一个链接的页面。当我使用链接转到test.php时,(下面的代码,它没有进行Fullname会话,但是如果我回去再次登录,它会有效吗?

session_start();
echo session_id();
echo "Name: " . $_SESSION["fullName"];

3 个答案:

答案 0 :(得分:1)

如果您的代码粘贴正确,则会出现错误:

public function SetUserSession($userName,$fullName)
{
    // Start session
    $this->StartSession;

    // Should be
    $this->StartSession();

- 更新 -

// landing page
session_start();
$_SESSION['test'] = 'I am hax0r';

// test page
session_start();
var_dump($_SESSION['test']); // Should output 'I am hax0r'

答案 1 :(得分:1)

放 在session_start(); 始终位于页面顶部!

所以这里它可能会起作用:

   <?
    session_start();
    include("useritemrepository.php");
    include('UserItem.php');
    include('sessionrepository.php');
    include('login.php');
    ...

因为:如果您包含的其他文件中有一个(或更多)的空格。 Session_start不起作用。此空间将写入输出,session_start无法修改页面的http标头。通常在发生这种情况时会收到错误/警告/通知。因此,启用您的错误或查找错误日志文件。自从你开始 - &gt;将警告和错误放在输出中,并确保在部署时禁用它们。

本质上是3件adivce:

  1. session_start总是先行
  2. 函数/类中没有html
  3. 从代码中获取html,并使用小变量包含“html / login.php”

答案 2 :(得分:0)

看着

public function CheckLogin($userName)
{
        $this->StartSession;
        if(empty($_SESSION[$userName]))
        {
            return false;
        }
        return true;
}

我认为它应该是:

public function CheckLogin($userName)
{
        $this->StartSession;
        if(empty($_SESSION['userName']))
        {
            return false;
        }
        return true;
}