2017-08-28 2 views
1

Wenn eine Person eingeloggt ist, kann nur sie ihre persönlichen Daten sehen. Wie kann ich ihm nur seine Daten in PHP zeigen?Benutzerdaten anzeigen nach Anmeldung 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

Scheint, Sie haben Ihre eigene Frage beantwortet? – Stuart

+0

Was ist das Problem jetzt? –

Antwort

0

Verwenden Sie diesen Code.

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']"; 
} 

Es ist besser, den Benutzernamen beim Anmelden als Sitzungsvariable festzulegen, so wie Sie die Benutzer-ID festgelegt haben. Dann müssen Sie keine Werte von der db abrufen. U kann nur den Namen aus der Sitzung anzeigen.

$_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 [ 'username'] = $ data [ 'username'];

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"); 
} 

?> 
Verwandte Themen