내 사이트에 새 회원이 등록되면 문제가 발생하여 프로필 페이지로 연결되어야하며 데이터가 표시됩니다.사용자가 등록 할 때 세션 변수가 저장되지 않습니다.
현재 "그 사용자가 존재하지 않습니다."라는 것을 제외하고는 프로필 페이지로 이동합니다. 그것은 세션 변수를 저장하지 않는 것 같습니다. 이 신입 회원을 로그 아웃하고 다시 로그인하면 모든 것이 새로운 세션에서 올바르게 작동합니다.
이 문제의 원인은 무엇입니까? 이 문제를 해결하는 데 도움이되는 몇 가지 코드가 있습니다. 더 많은 코드가 필요하면 알려주십시오. 참고로 경로는 register.php 페이지로 이동 한 다음 profile.php 페이지로 리디렉션되므로 profile.php 코드도 제공합니다.
기능은 다음과 같습니다 register.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}')");
}
코드는 다음과 같습니다
<?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);
}
<?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()'를 호출 하시겠습니까? –
아니요, 저는 register.php 나 profile.php에 불려지는 것을 가지고 있지 않았지만 방금 추가하려고 시도했지만 불행히도 문제를 해결하지 못했습니다. – soshannad