我正在制作一个网站,用户必须在其中登录/创建一个帐户,然后他们可以在自己的个人仪表板上输入术语以在 Twitter 上搜索。
为了确保他们在每个页面上都登录,我有以下代码:
<?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();
}
?>
在代码中,我尝试使用他们从登录页面登录的用户名
<?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>';
}
}
}
}
?>
然后最后在他们的仪表板上输入他们想要的条款,但我不知道如何知道谁从那里登录(我只是尝试使用第 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);
}
}
?>
$username 变量不起作用,我想知道我不应该将 checklogin 部分作为不同的页面。我对会话变量了解不多。