php - Retrieve Single User Data Using Session After He Logged In -
i new field , first time working session, question may seem basic appreciate if me. have made login , logout page using session , wish display data of particular user has logged in. user redirected retailer_login.php after sign in, apart login form there 4 pages entire login , logout process.
retailer_login.php, retailer_session.php, retailer_profile.php, retailer_logout.php
every page working fine able display single data column of user database wish display entire information stored specific user.
database
id name email password country city state occupation 1 sam sam@gmail.com sam xyz zbc qwe student
retailer_login page
<?php session_start(); // starting session if (isset($_post['submit'])) { try { if (empty($_post['email']) || empty($_post['password'])) { throw new exception("email or password invalid"); } else { // define $email , $password $email = $_post['email']; $password = $_post['password']; // protect mysql injection security purpose $email = stripslashes($email); $password = stripslashes($password); $mail = mysql_real_escape_string($email); $password = mysql_real_escape_string($password); //etablishing connection server passing server_name, user_id , password parameter $connection = mysql_connect("abc.com", "abc", "abc"); // selecting database $db= mysql_select_db("abc", $connection); // sql query fetch information of registerd users , finds user match. $query = mysql_query("select * retailerregister password='$password' , email='$email'", $connection); $rows = mysql_num_rows($query); if ($rows != 1) throw new exception("email or password invalid"); $_session['login_user'] = $email; // initializing session header("location: retailer_profile.php"); // redirecting other page mysql_close($connection); // closing connection } } catch (exception $e) { $_session['login_error'] = $e->getmessage(); header("location: index.html"); } } ?>
retailer_profile page
<?php include('retailer_session.php'); ?> <!doctype> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>welcome 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 page
<?php session_start(); if(session_destroy()) // destroying sessions { header("location: index.html"); // redirecting home page } ?>
retailer_session page
<?php // establishing connection server passing server_name, user_id , password 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 fetch complete information of user $ses_sql=mysql_query("select * retailerregister 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 home page } ?>
right able use $login_session
in order display email on profile page. can please tell how display other data of logged in user on retailer_profile page through session
just create variables current logged in user:
$row = mysql_fetch_assoc($ses_sql); $login_session =$row['email']; // user data $user_name = $row['name']; $user_country = $row['country']; $user_city = $row['city']; $user_state = $row['state']; $user_occupation = $row['occupation'];
or can use 1 variable shouldn't overwritten:
$user_data = $row;
and somewhere in script:
echo $user_data['city']; // etc...
Comments
Post a Comment