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