2014-02-19 161 views
0

这是我的第一个问题,所以请原谅我,如果我违背了一些职位礼仪 - 我会尽我所能解释我的问题清楚,我已经搜索了以前的问题,但没有任何符合我的问题到目前为止如我所知。登录后PHP会话似乎不工作! Wamp - 创建4个会话/登录?

背景:使用Apache 2.4.4和PHP 5.4.12在WAMP服务器2.4上运行 - 请让我知道你是否需要任何细节。

我一直在研究一个新的webapp项目,并且似乎在尝试获取PHP会话时遇到了问题。我的登录过程如下所示:

  • 一旦用户提交的细节和它们与存储在MySQL数据库中交叉检查,是创建一个会话,他们将被重定向到一个临时受保护的页面。

  • 临时页面检查用户是否有有效会话,如果有,则显示欢迎消息。

  • 如果用户没有有效的会话,他们会收到一个错误。

问题:每当我登录(成功可能我想补充),我重定向并收到错误消息“您无权访问此页面。”

下面是登录处理的代码(process_login.php):

<?php 
include_once 'db_connect.php'; 
include_once 'functions.php'; 

sec_session_start(); // Our custom secure way of starting a PHP session. 

if (isset($_POST['email'], $_POST['p'])) { 
$email = $_POST['email']; 
$password = $_POST['p']; // The hashed password. 


//Form data error handling. 
if ($email == "" || $password == ""){ 
    echo "login failed"; 
    exit(); 

} else { 
//DB stuff. 
$stmt = $mysqli->prepare("SELECT id, username, password, salt 
FROM members 
    WHERE email = ? 
    LIMIT 1"); 
$stmt->bind_param('s', $email); // Bind "$email" to parameter. 
$stmt->execute(); // Execute the prepared query. 
$stmt->store_result(); 

// get variables from result. 
$stmt->bind_result($user_id, $username, $db_password, $salt); 
$stmt->fetch(); 
// hash the password with the unique salt. 
$password = hash('sha512', $password . $salt); 
    if ($stmt->num_rows == 1) { 
     // If the user exists we check if the account is locked 
     // from too many login attempts 

     if (checkbrute($user_id, $mysqli) == true) { 
      // Account is locked 
      // Send an email to user saying their account is locked 
      return false; 
     } else { 
      // Check if the password in the database matches 
      // the password the user submitted. 
      if ($db_password == $password) { 
       // Password is correct! 
       // Get the user-agent string of the user. 
       $user_browser = $_SERVER['HTTP_USER_AGENT']; 
       // XSS protection as we might print this value 
       $user_id = preg_replace("/[^0-9]+/", "", $user_id); 
       $_SESSION['user_id'] = $user_id; 
       // XSS protection as we might print this value 
       $username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username); 

       $_SESSION['username'] = $username; 
       $_SESSION['login_string'] = hash('sha512', $password . $user_browser);  

       header('Location: ../protected_page.php'); 
      } else { 
       // Login failed 
       // Password is not correct 
       // We record this attempt in the database 
       $now = time(); 
       $mysqli->query("INSERT INTO login_attempts(user_id, time) 
           VALUES ('$user_id', '$now')"); 

       header('Location: ../index.php?error=1'); 
      } 
     } 
} 

这里是我在session_start函数的代码(sec_session_start())

function sec_session_start() { 
$session_name = 'sec_session_id'; // Set a custom session name 
$secure = true; 
// 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. 

这是我的临时测试代码(protected_pa​​ge.php); -note我是新手,似乎无法发布我的html。

<?php 
include_once 'includes/db_connect.php'; 
include_once 'includes/functions.php'; 
sec_session_start(); 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Secure Login: Protected Page</title> 
     <link rel="stylesheet" href="styles/main.css" /> 
    </head> 
    <body> 
     <?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. 
      </p> 
      <p>Return to <a href="index.php">login page</a></p> 
     <?php } else {?> 
      <p> 
       <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>. 
      </p> 
     <?php }?> 
    </body> 
</html>  

至于其他细节,可能有所作为 - 登录表单通过边栏加载并发送

任何帮助,非常感谢!我对这个东西是半新的,我已经花了5个多小时摆弄,似乎无法弄清楚。登录的作品,会话代码(据我所知)是有道理的,应该工作 - 嗨,哈哈我。

添加备注:我检查了我的C:/ wamp/etc /文件并清除了会话,只是通过登录显然会创建4个会话文件?我认为这一定与它有关。

http://puu.sh/71Lhm.png

+0

你可以输出'session_get_cookie_params'生成的数组吗? – dethtron5000

+0

@ dethtron5000嗨,我接受它我只是通过添加:'<?php $ array = session_get_cookie_params(); while(list($ key,$ val)= each($ array)){echo“$ key => $ val”; }如果是这样,这是输出;终身=> 0路径=> /域=>安全=> 1httponly => 1 puu.sh/71Mqo.png感谢您的帮助顺便说一句。 – Tainted

+0

'print_r(session_get_cookie_params());'也可以;)。 – dethtron5000

回答

0

好吧,我最终解决我自己的问题,但我要离开这里了答案柜面任何人有WAMP类似的问题。 (我认为这是因为我正在使用WAMP)

在我的自定义会话功能(sec_session_start)中,我选择了$ secure启用。尽管我相信只有在使用HTTPS的生产服务器上运行它时,它才有用。 (不在我的本地机器上。)我的推理可能是错误的,但我改变了价值

$secure = true; 

to 

$secure = false; 

它的工作!巨大的成功。