เมื่อผู้ใช้ลงทะเบียน ตัวแปรเซสชันจะไม่ถูกจัดเก็บ

บนเว็บไซต์ของฉัน ฉันมีปัญหาเมื่อสมาชิกใหม่ลงทะเบียน สมาชิกควรนำพวกเขาไปยังหน้าโปรไฟล์และข้อมูลของพวกเขาจะถูกดึงขึ้นมา

ขณะนี้ระบบจะพาพวกเขาไปที่หน้าโปรไฟล์ของตน ยกเว้นว่า "ไม่มีผู้ใช้รายนั้น" เพราะดูเหมือนว่าจะไม่ได้บันทึกตัวแปรเซสชัน ถ้าฉันออกจากระบบสมาชิกใหม่รายนี้แล้วกลับเข้าสู่ระบบใหม่ ทุกอย่างจะทำงานได้อย่างถูกต้องในเซสชันใหม่

อะไรทำให้เกิดสิ่งนี้? นี่คือโค้ดบางส่วนที่อาจช่วยแก้ปัญหาได้ หากคุณต้องการโค้ดเพิ่มเติม โปรดแจ้งให้เราทราบ โปรดทราบว่าเส้นทางคือพวกเขาไปที่หน้า 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 คือ: //ดึงข้อมูลโปรไฟล์สำหรับฟังก์ชันผู้ใช้ที่กำหนด 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();
    }
}



?>

person soshannad    schedule 19.01.2012    source แหล่งที่มา
comment
คุณกำลังโทรหา session_start() เหรอ?   -  person Explosion Pills    schedule 19.01.2012
comment
ไม่ ฉันไม่ได้เรียกสิ่งนั้นใน register.php หรือprofile.php แต่ฉันแค่ลองเพิ่มเข้าไป แต่น่าเสียดายที่ไม่สามารถแก้ไขปัญหาได้   -  person soshannad    schedule 19.01.2012


คำตอบ (2)


ขั้นแรก include('core/init.inc.php'); (หรือไฟล์อื่นๆ) มี session_start() หากไม่เรียก session_start() $_SESSION ของคุณจะว่างเปล่า

ประการที่สอง: ฉันไม่เคยเห็นคุณมอบหมาย $_SESSION['uid'] คุณเปลี่ยนเส้นทางไปยังโปรไฟล์ของคุณด้วย $_GET['uid'] หรือไม่?

เพราะใน register.php คุณกำลังตั้งค่า $_SESSION['username'] แล้วทำการเปลี่ยนเส้นทาง คุณอาจต้องการตั้งค่า $_SESSION['uid'] ที่นั่นก่อนทำการเปลี่ยนเส้นทาง

person pdu    schedule 19.01.2012
comment
ให้ฉันหาวิธีแสดงรหัสพิเศษให้คุณดู ฉันจะโพสต์มันในไม่ช้า - person soshannad; 19.01.2012
comment
ฉันยังเพิ่ม $_SESSION['uid'] = $_POST['user_id']; ก่อนการเปลี่ยนเส้นทางบน register.php แต่ไม่สามารถแก้ปัญหาได้ - person soshannad; 19.01.2012
comment
คุณช่วยกรุณากรอก var_dump($_SESSION) และตรวจสอบว่ามีชุด uid อยู่หรือไม่ - person pdu; 19.01.2012
comment
ถ้าฉันทำ vardump และลงทะเบียนผู้ใช้ใหม่ มันบอกว่าไม่มีโปรไฟล์อยู่ แต่ vardump บอกว่า: array(1) { [ชื่อผู้ใช้]=› string(4) test } และ test คือชื่อของบัญชีใหม่ ฉันทำ. - person soshannad; 19.01.2012
comment
ฉันเพิ่มคำขอเพื่อรับ uid และตอนนี้มันบอกว่า: array(2) { [username]=› string(5) test2 [uid]=› NULL } คำขอที่ฉันเพิ่มคือ: $_SESSION['uid'] = $ _GET['uid']; - person soshannad; 19.01.2012
comment
@soshannad ฉันคิดว่าคุณกำลังยุ่งนิดหน่อย หายใจเข้าแล้วลองอีกครั้ง ;-) หากคุณไม่มี GET หรือ POST สำหรับ uid แสดงว่าคุณกำลังกำหนดค่า null คุณสามารถแก้ไข add_user(); เพื่อส่งคืน uid ที่ผู้ใช้เพิ่มมีและกำหนดสิ่งนี้ให้กับเซสชัน var ก่อนที่จะเปลี่ยนเส้นทาง - person pdu; 19.01.2012

ก่อนอื่นให้ลองสิ่งนี้ใน 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() ในทุกเพจที่ต้องเข้าถึงเซสชัน

หวังว่ามันจะช่วยได้!

person capi    schedule 19.01.2012