2012-01-19 10 views
0

私のサイトでは、新しいメンバーが登録されると問題が発生し、プロフィールページに表示され、データがプルアップされるはずです。ユーザーが登録すると、セッション変数が保存されません

現在、「そのユーザーは存在しません」ということを除いて、プロフィールページに表示されます。なぜなら、セッション変数を保存していないようだからです。この新しいメンバーをログアウトしてログインし直すと、新しいセッションですべて正常に動作します。

この原因は何ですか?問題の解決に役立つコードがいくつかあります。コードがもっと必要な場合は教えてください。ほんのちょうど、パスは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;

<?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(); 
    } 
} 



?> 
+0

あなたは 'session_start()'を呼び出していますか? –

+0

いいえ、私はregister.phpまたはprofile.phpで呼び出されていませんでしたが、私はそれを追加しようとしましたが、残念ながら問題を修正しませんでした。 – soshannad

答えて

0

まずinit.inc.php

$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); 
} 

include('core/init.inc.php');(またはいくつかの他のファイル)を行い、あなたの$_SESSIONが空になりますsession_start()呼び出すことなくsession_start()が含まれています。

2番目:$_SESSION['uid']を割り当てることはありません。 $_GET['uid']であなたのプロフィールにリダイレクトしますか?

registration.phpで$_SESSION['username']を設定してから、リダイレクトを行うためです。リダイレクトを行う前に$_SESSION['uid']と設定することもできます。

+0

あなたに余分なコードを表示する方法を教えてください。まもなく投稿します。 – soshannad

+0

これを上に追加しました。 – soshannad

+0

また、$ _SESSION ['uid'] = $ _POST ['user_id'];を追加しました。 register.phpのリダイレクトの前に、それは問題を解決しませんでした。 – soshannad

0

まず、ADD_USERでこれを試してみてください。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}')"); 
    return mysql_insert_id(); 
} 

第二に、:

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

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

あなたが(にsession_startを持っていることを確認してください)セッションにアクセスするために必要なすべてのページで。

希望すると助かります!

関連する問題