【问题标题】:profile page not working with session id个人资料页面不适用于会话 ID
【发布时间】:2013-05-02 01:20:22
【问题描述】:

我正在创建一个配置文件页面和一个登录页面,我在其中存储会话 ID,然后在配置文件文件中我检查是否 isset 但我得到的问题是系统总是显示错误消息并且我使用 print_r($_SESSION); 浏览器显示:

重要数据丢失Array ( [first_name] => [email] => )

如何解决这个错误??????

login.php

<?php
session_start();
error_reporting(E_ALL);
require_once('include/connect.php');
$message = ""; 
if(!empty($_POST['email']))
{

$email = $_POST['email'];
$pass = $_POST['pass'];

$email = strip_tags($email);
$pass = strip_tags($pass);
$email = mysql_real_escape_string($email);
$pass = mysql_real_escape_string($pass);
//$pass = md5($pass);

$sql=mysql_query( "SELECT user_id, email_address, first_name FROM user WHERE email_address='$email'AND password='$pass'LIMIT 1") or die("error in user table");
$login_check = mysql_num_rows($sql);

  if($login_check > 0)
  {
      $row = mysql_fetch_array($sql);

          $id = $row['user_id'];
          $_SESSION['user_id'] = $id;

          $firstname = $row['first_name'];
          $_SESSION['first_name']= $firstname;

          $email = $row['email_address'];
          $_SESSION['email_address']= $email;

          mysql_query("UPDATE user SET last_log_date=now() WHERE user_id='$id'");

          header("Location: profile.php");    
  }//close if 
  else
  {
      $message = "incorrect Email or Password!!";
      //exit();
  }
}//close if

?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RegisterPage</title>

<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Abel|Satisfy' rel='stylesheet' type='text/css' />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />

</head>

<body>


       <div id="loginborder">
         <p  style="color:#FF0000" align="left"><?php print("$message") ?></p>

         <!--Login form where user submit his registered email and password-->
         <form action="login.php" method="post">
           email-address:<br />
           <input type="text" name="email" placeholder="Email Adress" />
           <br />
           <br />
           Password:<br />
           <input type="password" name="pass" placeholder="Password" />
           <br />
           <br />
           <input type="submit" name="login" value="Login" />
           <a href="register.php" style="position: absolute; top: 132px; left: 61px;"> <strong> Register</strong></a>
         </form>
       </div> 

profile.php

   <?php
session_start();
 require_once('include/connect.php'); 


if(isset($_GET['user_id']))
{
    $id=$_GET['user_id'];
    var_dump($id);

}
elseif(isset($_SESSION['user_id']))
{
    $id= $_SESSION['user_id'];
}

else
{
    print "Important  data  are missing";
    print_r($_SESSION);
    exit();

}

$sql = mysql_query("SELECT * FROM user  WHERE user_id='$id'") or die(mysql_error());
$row = mysql_fetch_array($sql);

   $firstname=$row['first_name'];
   $lastname=$row['last_name'];
   $birth_date=$row['birth_date'];
   $registered_date=$row['registered_date'];
   //***************for upload img*****************//
   $check_pic="members/$id/image01.jpg";
   $default_pic="members/0/image01.jpg";
   if(file_exists($check_pic))
   {
       $user_pic="<img src=\"$check_pic\"width=\"100px\"/>";
   }
   else
   {
       $user_pic="<img src=\"$default_pic\">";
   }
   echo $id, $firstname, $birth_date;
?>

【问题讨论】:

  • 你只在创建会话时选择 user_id,我猜错误是从那里 "SELECT user_id FROM user" => "SELECT user_id, first_name, email FROM user"
  • 你的意思是我还必须选择user_id???
  • 不,当您设置会话时,您使用 $row['first_name'] 和 $row['email'] 但它们不会在您的行中返回,因为您只是从 SELECT 中获取 user_id ,我说的对吗?
  • 是的,我只使用了 user_id
  • 也改变 $id=$_SESSION['user_id'];到 $_SESSION['user_id'] = $id;

标签: php mysql session


【解决方案1】:

你需要改变几件事

First : 在您的请求中获取 first_name 和电子邮件

'SELECT user_id,email,first_name FROM user WHERE email_address='$email'AND password='$pass'LIMIT 1'

其次,去掉while循环,做

$row = mysql_fetch_array($sql);

您限制为 1 个结果,因此无需在结果内循环

$id=$_SESSION['user_id']; 更改为$_SESSION['user_id'] = $id;

此外,将配置文件的结果限制为 1 并删除循环(user_id => UNIQUE => LIMIT 1)

【讨论】:

    【解决方案2】:

    所有你需要做的只是在一个会话变量 [$_SESSION['username']] 中存储一个值,然后使用会话中的值从 mysql 表中选择数据

    ----------------------例如----------- -------------------------------------------------------

    关于 login.php

    if($login_check > 0)
    {
      $_SESSION['email']=$email;//storing variable in SESSION
     header("Location: profile.php");    
    }
    else
    {
      $message = "incorrect Email or Password!!";
      die();// kill the script
    }
    

    在 profile.php 上

    <?php
    session_start();// start session
    require_once('include/connect.php'); //include connection file
    
    $sql = mysql_query("SELECT * FROM user  WHERE email='(mysql_real_escape_string($_SESSION['email']))'") or die(mysql_error());
    $row = mysql_fetch_array($sql);
    // then just echo all the data you need
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2015-10-08
      • 1970-01-01
      • 2020-01-23
      相关资源
      最近更新 更多