2014-10-20 53 views
0

好吧我不知道我在做什么错误。我过去也使用过SESSION变量,但没有任何问题,但这次似乎不起作用。PHP SESSION阵列从一个文件夹重定向到另一个文件夹时被删除

这里是我的protected_pa​​ge.php(登录成功后用户重定向的页面)的代码。

<?php if (login_check($mysqli) == true) : ?> 
     <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p> 
     <p> 
      This is an example protected page. To access this page, users 
      must be logged in. At some stage, we'll also check the role of 
      the user, so pages will be able to determine the type of user 
      authorised to access the page. 
      </br></br> 
      <a href="add/home.php">Add new user!</a> 
     </p> 
     <p>Logout & return to <a href="includes/logout.php">login page</a></p> 
    <?php else : ?> 
     <p> 
      <span class="error">You are not authorized to access this page.</span> Please <a href="includes/logout.php">login</a>. 
     </p> 
    <?php endif; ?> 

函数login_check()检查天气用户已经与正确的凭证或没有登录正常。如果是,则返回true。

还有另一个函数,即login(),它在登录过程中被调用,并使用输入的凭证匹配凭证。如果为true,则它将'用户名'存储在$ _SESSION变量中。

当我点击“添加新用户!”它发送给我添加/ home.php页面,但有$ _SESSION变量。

if (login_check($mysqli) == true) : ?> 
    <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p> 
    <h1>Add user!</h1> 
    <?php 
    if (!empty($error_msg)) { 
     echo $error_msg; 
    } 
    ?> 
    <ul> 
     <li>Emails must have a valid email format</li> 
    </ul> 
    <form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" 
      method="post" 
      name="registration_form"> 
     Full Name: <input type='text' 
      name='fullname' 
      id='fullname' /><br> 
     Email: <input type="text" name="email" id="email" /><br> 
     Project Name: <input type="text" 
         name="project" 
         id="project"/><br> 
     Phone Number: <input type="text" 
           name="phone" 
           id="phone" /><br> 
     <input type="submit" 
       value="Submit" /> 
    </form> 
    <p>Logout & return to <a href="../includes/logout.php">login page</a></p> 
    <?php else : echo "7"; ?> 
     <p> 
      <span class="error">You are not authorized to access this page.</span> Please <a href="../includes/logout.php">login</a>. 
     </p> 
    <?php endif; ?> 

它总是打印我没有足够的凭据来查看此页面。

我还在这两个页面上引入了sec_session_start()函数。 (protected_pa​​ge.php & add/home.php)。 sec_session_start()是用户定义的自定义会话启动函数。

function sec_session_start() { 
$session_name = 'sec_session_id'; // Set a custom session name 
$secure = SECURE; 
// This stops JavaScript being able to access the session id. 
$httponly = true; 
// Forces sessions to only use cookies. 
if (ini_set('session.use_only_cookies', 1) === FALSE) { 
    header("Location: ../error.php?err=Could not initiate a safe session (ini_set)"); 
    exit(); 
} 
// Gets current cookies params. 
$cookieParams = session_get_cookie_params(); 
session_set_cookie_params($cookieParams["lifetime"], 
    $cookieParams["path"], 
    $cookieParams["domain"], 
    $secure, 
    $httponly); 
// Sets the session name to the one set above. 
session_name($session_name); 
session_start();   // Start the PHP session 
session_regenerate_id(); // regenerated the session, delete the old one. 

}

我不知道我做错了这个时候。一定是一个愚蠢的错误。提前致谢。 :)

欲了解更多详情请通知我。

回答

0

您需要在页面顶部的所有.php页面上有session_start()。

确保你有它。即使你在一个功能中有这个功能,它可能会把它放到最底层。

你有这样的事情:

session_name($session_name); 
session_start();   // Start the PHP session 
session_regenerate_id(); // regenerated the session, delete the old one. 

您必须在session_start为页面上的第一件事。确保功能 没有离页面顶端太远。

+0

我的sec_session_start()在文件functions.php中,我已经将它包含在每个php文件的顶部。在include_once''之后,我的第一行始终是sec_session_start()。我仍然得到错误。 – 2014-10-20 18:39:46

+0

看起来像sec_session_start在一堆代码之后调用session_start。尝试将其移到顶端或摆脱它,并将其放在函数之外,以绝对首先测试出来解决问题。 – unixmiah 2014-10-20 18:42:44

+0

好的,这是我做的。我摆脱了include_once'functions.php',而是在'add/home.php'内复制了sec_session_start()和login_check()的内容,但问题仍然存在。我甚至使用session_start()而不是sec_session_start(),但没用。 – 2014-10-20 18:51:19

相关问题