简体   繁体   English

PHP在页面上检索当前用户的用户名,然后在sql查询中使用它

[英]PHP retrieving username of current user on page and then using it in a sql query

I am making a website where a user has to log in/create an account and then they can type in terms to search on twitter on their own personal dashboard. 我正在建立一个网站,用户必须登录/创建一个帐户,然后他们才能输入术语以在自己的个人仪表板上搜索Twitter。

To make sure they are logged in on every page I have this code: 为了确保他们在每个页面上都登录,我有以下代码:

<?php
session_start();

if (isset($_SESSION['loggedin']))
{
$_SESSION["username"] = $username;
    echo "(You are logged in)";
}
else
{

echo "(Not logged in)";
echo"<p><a href = 'login.php'>Log In</a>";
echo"<p><a href = 'createaccount.php'>Create Account</a>";

    exit();
}
?>

In the code I try and take the username they logged in with from the login page 在代码中,我尝试使用他们从登录页面登录时使用的用户名

    <?php

session_start();

    //connect to database
$db = mysqli_connect("number", "name",
"password");
//msg if not connected 
if (!$db)
{
echo "Sorry!I just can't connect to database";
}
if(isset($_POST['submit']))  {

$username =$_POST['username'];
$password = $_POST['password'];

if(empty($username) OR empty ($password)) {

echo "you missed something";
}

else {

if(!empty($username) && !empty($password)) {

mysqli_select_db ($db,"name");

$qry=" SELECT username FROM login WHERE username= '$username' AND password = '$password';";

$result = mysqli_query($db,$qry);
$num_rows = mysqli_num_rows($result);

if(($num_rows) == 1) {
            //username Successful
            header("Location:index.php");
$_SESSION['loggedin'] = 'yes';
    $username = $_SESSION['username'];

        }

        else {
            //username failed
print '<script type="text/javascript">';
print 'alert("Incorrect infomation. Try again.")';
print '</script>';              
        }


}


}

}

?>

And then finally on their dashboards they enter the terms they want but I can't figure out how to know who is logged in from there (im just trying with term 1 atm) 最后,在他们的仪表板上,他们输入所需的条款,但我不知道如何知道谁从那里登录(即只是尝试使用条款1 atm)

<?php   
session_start(); 

//connect to database
$db = mysqli_connect("number", "name",
"password");
//msg if not connected 
if (!$db)
{
echo "Sorry!I just can't connect to database";
}
$term1 =$_POST['term1'];
$term2 = $_POST['term2'];
$term3 = $_POST['term3'];
$_SESSION["username"] = $username;

if(isset($_POST['submit']))  {


mysqli_select_db ($db,"sarahpattison");

$query2 = "INSERT INTO terms (term) VALUES ('$term1') ;";

$result = mysqli_query($db,$query2);
$term1id = mysqli_insert_id($db);

echo $term1id;
if ($result){

$qry5="INSERT into userterms(userid,termid) SELECT login.userid, terms.termid FROM login, terms WHERE login.username = '$username' AND terms.termid='$term1id';";
$result8 = mysqli_query($db,$qry5);
}



}



?>

The $username variable isn't working and Im wondering is it that I shouldn't have the checklogin part as a different page. $ username变量不起作用,我想知道是我不应该将checklogin部分作为其他页面使用。 I don't know much about session variables. 我对会话变量了解不多。

Try 尝试

$username = $_SESSION["username"];

instead of 代替

$_SESSION["username"] = $username;

In your third file 在你的第三档

Disclaimer : This answer does not directly reply to your question/issue, but solves it by implementing a better architecture in your system. 免责声明 :此答案不会直接回答您的问题/问题,而是通过在您的系统中实现更好的体系结构来解决。

You should not take the username from the login page, instead, query your DB for the user ID that you should be storing in the session. 您不应从登录页面获取用户名,而应在数据库中查询应存储在会话中的用户ID。

Your login system architecture should follow this: 您的登录系统架构应遵循以下原则:

  1. Get the username and password from the login form; 从登录表单获取用户名和密码;
  2. Query the DB looking for the ID of and user with the username and password above; 查询数据库以查找上面的用户名和密码的ID和用户;
  3. Store the user ID in the Session like $_SESSION['userId'] = (some id returned from DB) 将用户ID像$_SESSION['userId'] = (some id returned from DB)一样存储在会话中

Then everytime you need to know the username, just query the DB again. 然后,每次您需要知道用户名时,只需再次查询数据库即可。 This will avoid issues when the user changes it's username and will keep your system more secure. 这样可以避免用户更改用户名时出现问题,并使您的系统更安全。

Then you can remove this code: 然后,您可以删除以下代码:

$_SESSION['loggedin'] = 'yes';
$username = $_SESSION['username'];

And just check if $_SESSION['userId'] isset() to know if the user is logged in or not. 只需检查$_SESSION['userId'] isset()即可知道用户是否登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM