[英]PHP, Only select logged in username from database
我基本上希望它僅選擇已登錄的當前用戶,然后在此處顯示用戶名和點,而不是顯示數據庫中的所有用戶
這是顯示它的代碼
<?php
include("connect.php"); // Includes the file connect.php to connect to database
session_start(); // Starting session cookies
if($_SESSION['LOGGEDIN'] == 1) //Checking if they have the session cookie
{
$result = mysql_query("SELECT * FROM userdata");
echo "<table border='1'>
<tr>
<th>Username</th>
<th>Points</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
else
{
echo "<title>Error!</title>";
//Doesn't have session cookie
echo "YOU ARE NOT LOGGED IN!";
}
?>
好吧,您正在選擇整個表格。
只需將其更改為
$result = mysql_query("SELECT * FROM `userdata` WHERE `username`='".$_SESSION["LOGGEDIN"]."' LIMIT 1");
請注意,您應該更改會話變量LOGGEDIN
以包含已登錄用戶的用戶名,或者使用另一個會話變量,並替換我在上面的代碼行中對LOGGEDIN
引用。
例如,在您的登錄腳本中,不要執行以下操作:
if($_POST["user"] == $user and $_POST["password"] == $pass)
$_SESSION["LOGGEDIN"] = 1;
做這個:
if($_POST["user"] == $user and $_POST["password"] == $pass)
$_SESSION["LOGGEDIN"] = $user;
如果確實使用LOGGEDIN
,則將需要更新初始的if子句,以便它不會檢查它是否等於1,而是檢查它是否已設置:
if(isset($_SESSION["LOGGEDIN"]))
因此,您的文件應如下所示:
<?php
include("connect.php"); // Includes the file connect.php to connect to database
session_start(); // Starting session cookies
if(isset($_SESSION['LOGGEDIN'])) //Checking if they have the session cookie
{
$result = mysql_query("SELECT * FROM `userdata` WHERE `username`='".$_SESSION["LOGGEDIN"]."' LIMIT 1");
echo "<table border='1'>
<tr>
<th>Username</th>
<th>Points</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
else
{
echo "<title>Error!</title>";
//Doesn't have session cookie
echo "YOU ARE NOT LOGGED IN!";
}
?>
<?php
include("connect.php");
$email= $_POST['userid'];
$password= $_POST['password1'];
$papas=base64_encode($password);
$check = $_POST['rememberme'];
$tablename="userdata";
$select_qry = $jeob->SqlQuery("SELECT * FROM ".$jeob->dbprefix.$tablename." WHERE email ='$email' AND password ='$papas' AND active_link='1' ");
if($jeob->SqlRows($select_qry) == "0"){
echo "Invalid Username and Password";
} else {
$getuser = $jeob->SqlFetch($select_qry);
$_SESSION['userid'] = $getuser['user_id'];
$_SESSION['oauth_provider'] = "normal";
$_SESSION['email'] = $getuser['email'];
}
if($_SESSION['userid'] == ""
{
echo "You are not logged in";
}
else
{
Welcome "Fetch username using the session id or emial"
}
?>
希望這對您有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.