2017-08-28 112 views
1

當一個人登錄時,只有他可以看到他的個人數據。我怎樣才能在PHP中向他展示他的數據?登錄後顯示用戶數據php

<?php 
    include_once("connection.php"); 
    if($_SESSION['id']) 
    { 
     $_SESSION = ['id']; 
     $result = mysqli_query($connection, "select * from `student` where 
     `id` = $_SESSION[id]"); 
     $show=mysqli_fetch_assoc($result); 
     if($show) 
     { 
      echo "welcome [username]"; 
     } 
    } 
?> 
+0

似乎你已經回答了您自己的問題? – Stuart

+0

現在有什麼問題? –

回答

0

使用此代碼。

if($_SESSION['id']) 
{ 
$SESSION = $_SESSION['id']; 
$result = mysqli_query($connection, "select * from `student` where 
`id` = '$SESSION'"); 
$show=mysqli_fetch_assoc($result); 
echo "welcome $show['username']"; 
} 

最好在登錄時將用戶名設置爲會話變量,就像設置用戶標識一樣。那麼你將不需要從數據庫獲取值。你可以只顯示會話的名字。

$_session['username'] 
0
are us setting the session data at some place if yes then get the session 
data with the keys you have saved in session such as: 

echo $_SESSION['username']; 
echo $_SESSION['email']; 
echo $_SESSION['gender']; 

if you did not set the data at any place first set it in session super 
global and then get it Example: 

//set user data in session variable. 
//get data from database or any other data source 
// set in session 

$ _SESSION [ '用戶名'] = $數據[ '用戶名'];

0
<?php session_start(); 

//Connect to database from here 
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
//select the database | Change the name of database from here 
mysql_select_db('aasala'); 

//get the posted values 
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES); 
$pass=md5($_POST['password']); 

//now validating the username and password 
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'"; 
$result=mysql_query($sql); 
$row=mysql_fetch_array($result); 

//if username exists 
if(mysql_num_rows($result)>0) 
{ 
//compare the password 
if(strcmp($row['password'],$pass)==0) 
{ 

echo "yes"; 
//now set the session from here if needed and query the real name 
$_SESSION['u_name']=$user_name; 




} 
else 
echo "no"; 
} 
else 
echo "no"; //Invalid Login 


?> 

An the secure.php is this one 

<?php session_start(); 

// if session is not set redirect the user 
if(empty($_SESSION['u_name'])) 
header("Location:index.html"); 

//if logout then destroy the session and redirect the user 
if(isset($_GET['logout'])) 
{ 
session_destroy(); 
header("Location:index.html"); 
} 

?> 
相關問題