登录后使用会话检索单个用户数据

时间:2014-09-10 17:32:26

标签: php mysql sql session phpmyadmin

我是这个领域的新手,这是我第一次参加会议,这个问题似乎很基本,但如果有人能帮助我,我会很感激。目前我已经使用会话进行了登录和注销页面,并希望显示已登录的特定用户的数据。用户登录后被重定向到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   sam@gmail.com  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");
    }
}
?>

retaililer_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 个答案:

答案 0 :(得分: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...