首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >登录脚本中的会话问题

登录脚本中的会话问题
EN

Stack Overflow用户
提问于 2012-10-24 21:37:43
回答 1查看 91关注 0票数 0

在这个网站的帮助下,我现在已经让我的登录脚本在很大程度上工作了,但是当我试图检查一个受限制的页面上的会话时,有一个问题,完整的代码如下。

登录脚本本身

代码语言:javascript
复制
    <?php

function validateUser()
{

    session_regenerate_id (); //this is a security measure
    $_SESSION['valid'] = 1;
    $_SESSION['userid'] = $userid;
}


?>

<?php

ob_start(); // Start output buffering

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

session_start(); //must call session_start before using any $_SESSION variables
$username = $_POST['username'];
$password = $_POST['password'];
//connect to the database here

$hostname_PropSuite = "localhost";
$database_PropSuite = "propsuite";
$username_PropSuite = "root";
$password_PropSuite = "root";
$PropSuite = mysql_pconnect($hostname_PropSuite, $username_PropSuite, $password_PropSuite) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_PropSuite, $PropSuite);

$username = mysql_real_escape_string($username);

$query = "SELECT password, salt FROM admin_users WHERE username = '$username';";

$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result) < 1) //no such user exists
{
    header('Location: http://localhost/PropSuite/index.php?login=fail');

    die();
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
if($hash != $userData['password']) //incorrect password
{
    header('Location: http://localhost/PropSuite/index.php?login=fail');

    die();
}
else
{
   validateUser(); //sets the session data for this user
}
//redirect to another page or display "login success" message
header('Location: http://localhost/PropSuite/main');
die()




//redirect to another page or display "login success" message


?>

我试图限制的页面。

代码语言:javascript
复制
<?php

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);



function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        return true;
    return false;
}

//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: http://localhost/PropSuite/index.php');
    die();
}
//page content follows
?>

当我按下log时,它会把我带回登录页面,所以它看起来像是通过登录脚本,然后当它到达主页时,它会把我带回登录页面。我是confused.com :-(

预先感谢您的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-24 21:44:37

在您试图限制的页面中,我看不到session_start

代码语言:javascript
复制
<?php 
  session_start();
  error_reporting(E_ALL & ~E_NOTICE);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);
  .....
?>

顺便说一句,在你的其他脚本中,你应该这样写

代码语言:javascript
复制
 <?php
     .....
     $username = isset($_POST['username'])?$_POST['username']:'';
     $password = isset($_POST['password'])?$_POST['password']:'';
    ...
  ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13050460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档