2014-09-10 88 views
2

我是這個領域的新手,這是我第一次與會話一起工作,這個問題可能看起來很基本,但是如果有人能幫助我,我將不勝感激。目前,我已經使用會話創建了登錄和註銷頁面,並希望顯示已登錄的特定用戶的數據。用戶在登錄後重定向到retailer_login.php,除登錄表單外,整個登錄有4頁和註銷過程。在登錄後使用會話檢索單個用戶數據

retailer_login.php,retailer_session.php,retailer_profile.php,retailer_logout.php

每一頁都工作正常,但是我能夠顯示來自數據庫的用戶只有單一數據列,但我想顯示整個存儲有關該特定用戶的信息。

DATABASE

Id name email   password country city state occupation 
    1 sam [email protected] sam  XYZ  ZBC QWE student 

retailer_login頁

<?php 
session_start(); // Starting Session 

if (isset($_POST['submit'])) { 
    try { 
     if (empty($_POST['email']) || empty($_POST['password'])) { 
      throw new Exception("email or Password is invalid"); 
     } else { 
      // Define $email and $password 
      $email  = $_POST['email']; 
      $password = $_POST['password']; 
      // To protect MySQL injection for Security purpose 
      $email  = stripslashes($email); 
      $password = stripslashes($password); 
      $mail  = mysql_real_escape_string($email); 
      $password = mysql_real_escape_string($password); 
     //Etablishing Connection with Server by passing server_name, user_id and password as a parameter 
      $connection = mysql_connect("abc.com", "abc", "abc"); 
      // Selecting Database 
      $db= mysql_select_db("abc", $connection); 
      // SQL query to fetch information of registerd users and finds user match. 
      $query = mysql_query("select * from retailerregister where password='$password' AND email='$email'", $connection); 
      $rows = mysql_num_rows($query); 

      if ($rows != 1) 
       throw new Exception("email or Password is invalid"); 

      $_SESSION['login_user'] = $email; // Initializing Session 
      header("location: retailer_profile.php"); // Redirecting To Other Page 
      mysql_close($connection); // Closing Connection 
     } 
    } 
    catch (Exception $e) { 
     $_SESSION['login_error'] = $e->getMessage(); 
     header("Location: index.html"); 
    } 
} 
?> 

retailer_profile頁

<?php 
include('retailer_session.php'); 
?> 

<!DOCTYPE> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Welcome to your homepage</title> 

     <meta name="viewport" content="width=device-width", initial-scale=1.0"> 
     <link href="css/bootstrap.min.css" rel="stylesheet" /> 
     <link href="css/styles.css" rel="stylesheet" /> 
     <link href="css/carousel.css" rel="stylesheet"> 
     <link href="css/style.css" rel="stylesheet"> 
</head> 

<body> 
<div id="profile"> 
    <div class="navbar navbar-inverse navbar-static-top"> 
     <div class="container"> 
      <a href="#" class = "navbar-brand"> <id="welcome">Welcome : <i><?php echo $login_session; ?></i> </a> 

       <button class = "navbar-toggle" data-toggle = "collapse" data-target = ".navHeaderCollapse"> 
        <span class = "icon-bar"> </span> 
        <span class = "icon-bar"> </span> 
        <span class = "icon-bar"> </span> 
       </button> 

      <div class="collapse navbar-collapse navHeaderCollapse"> 
       <ul class = "nav navbar-nav navbar-right"> 
        <li class ="active"> <a href="retailer_profile.php">Home</a></li> 
        <li> <a href="#">Profile</a></li> 
        <li class="dropdown"> 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Property <b class ="caret"></b></a> 
          <ul class="dropdown-menu"> 
           <li> <a href="retailer_property_add.php">Add property</a> </li> 
           <li> <a href="#">View property</a> </li> 
          </ul> 
        </li> 
        <li> <a href="retailer_logout.php"><id="logout">Log Out</a></li> 
       </ul> 
      </div> 
     </div>  
    </div>    
</div> 

<div name="container"> 


</div> 

<script src = "js/jquery-1.11.1.js"> </script> 
<script src = "js/bootstrap.js"> </script> 

</body> 
</html> 

retailer_logout頁

<?php 
    session_start(); 
    if(session_destroy()) // Destroying All Sessions 
    { 
    header("Location: index.html"); // Redirecting To Home Page 
    } 
    ?> 

retailer_session頁

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("abc.com", "abc", "abc"); 
// Selecting Database 
$db = mysql_select_db("abc", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select * from retailerregister where email='$user_check'", $connection); 

$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['email']; 

if(!isset($login_session)){ 
mysql_close($connection); // Closing Connection 
header('Location: index.html'); // Redirecting To Home Page 
} 
?> 

現在我只能以個人資料頁上顯示的電子郵件使用$login_session。誰能告訴我如何通過會話顯示已登錄的用戶的retailer_profile頁面上的其他數據

回答

1

只需創建關於當前另一個變量登錄的用戶:

$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['email']; 

// another user data 

$user_name = $row['name']; 
$user_country = $row['country']; 
$user_city = $row['city']; 
$user_state = $row['state']; 
$user_occupation = $row['occupation']; 

或者你也可以使用一個變量,它不應該被改寫:

$user_data = $row; 

然後在某處腳本:

echo $user_data['city']; // etc...