用户注册时,不存储会话变量

时间:2012-01-19 07:31:55

标签: php session

在我的网站上,我有一个问题,当一个新成员注册时,它应该将他们引导到他们的个人资料页面,他们的数据应该拉起来。

目前它确实将他们带到了他们的个人资料页面,除了它说“那个用户不存在”。因为它似乎没有保存会话变量。如果我将这个新成员记录下来并重新登录,那么一切都可以正常使用新会话。

造成这种情况的原因是什么?以下是一些可能有助于解决问题的代码 - 如果您需要更多代码,请告诉我们。只是前面的路径是他们进入register.php页面,然后被重定向到profile.php页面,所以我也将提供profile.php代码。

功能是:

//adds a user to the database
    function add_user($user, $pass){
    $user = mysql_real_escape_string(htmlentities($user));
    $pass = sha1($pass);   
    mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}',     '{$pass}')");
    }

register.php的代码是:

<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
if (empty($_POST['username'])){

    $errors[] = 'The username cannot be empty.';    
}

if (empty($_POST['password']) || empty($_POST['repeat_password'])){

    $errors[] = 'The password cannot be empty.';
}

if ($_POST['password'] !== $_POST['repeat_password']){

    $errors[] = 'Password verification failed.';
}

if (user_exists($_POST['username'])){

    $errors[] = 'The username you entered is already taken.';
}

if (empty($errors)){
    add_user($_POST['username'], $_POST['password']);

    $_SESSION['username'] = htmlentities($_POST['username']);

    header('Location: profile.php');
    die();
}
}
?>

profile.php代码是:

<?php 

include('core/init.inc.php');    
if (isset($_GET['uid']) && $_GET['uid'] !='')
{
$user_info = fetch_user_info($_GET['uid']);
}
else
{
$user_info = fetch_user_info($_SESSION['uid']);
}    
?>

函数fetch_user_info是:     //获取给定用户的配置文件信息     function fetch_user_info($ uid){         $ uid =(int)$ uid;

    $sql = "SELECT
                                    `user_name` AS `username`,
                                    `user_firstname` AS `firstname`,
                                    `user_lastname` AS 'lastname',
                                    `user_email` AS `email`,
                                    `user_about` AS `about`,
                                    `user_location` AS `location`,
                                    `user_gender` AS `gender`
                            FROM `users`
                            WHERE `user_id` = {$uid}";
    $result = mysql_query($sql);
    return mysql_fetch_assoc($result);
}

init.inc.php

<?php

session_start();

$exceptions = array('register', 'login', 'find', 'profile');

$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

mysql_connect('localhost', 'root', 'root');
mysql_select_db('user_system');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");


if (isset($_COOKIE['username'], $_COOKIE['password']) && isset($_SESSION['username']) === false){

    if ($uid = valid_credentials($_COOKIE['username'], $_COOKIE['password'])){

            $_SESSION['username'] = htmlentities($_COOKIE['username']);
            $_SESSION['uid'] = $uid;
            setcookie('username', $_COOKIE['username'], time() + 604800);
            setcookie('password', $_COOKIE['password'], time() + 604800);
    }
}

if (in_array($page, $exceptions) === false){

 if (isset($_SESSION['username']) === false){

           header('Location: login.php');
           die();
    }
}



?>

2 个答案:

答案 0 :(得分:0)

首先,include('core/init.inc.php');(或其他一些文件)包含session_start()如果不调用session_start(),您的$_SESSION将为空。

第二:我从未见过你分配$_SESSION['uid']。您是否使用$_GET['uid']重定向到自己的个人资料?

因为在registration.php中,您正在设置$_SESSION['username'],然后进行重定向。在进行重定向之前,您可能还想在那里设置$_SESSION['uid']

答案 1 :(得分:0)

首先,在add_user中尝试:

//adds a user to the database
function add_user($user, $pass){
    $user = mysql_real_escape_string(htmlentities($user));
    $pass = sha1($pass);   
    mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}',     '{$pass}')");
    return mysql_insert_id();
}

其次,在register.php中:

[...]
if (empty($errors)){
    $_SESSION['uid'] = add_user($_POST['username'], $_POST['password']);
    $_SESSION['username'] = htmlentities($_POST['username']);

    header('Location: profile.php');
    die();
}

确保在需要访问会话的所有页面中都有session_start()。

希望它有所帮助!