PHP $_SESSION problem


PHP $_SESSION problem

我正在为我的网站制作一个登录页面。这样做也是因为它不会这样做,而不是因为它是安全的。我得到一个错误在第30行"$_SESSION['username']=$username;- "Parse error: syntax error, unexpected T_VARIABLE"

将所有数据库信息命名为"db12345"以隐藏凭据。此外,指向此登录页面的唯一点是使用名为"members"的数据库表将用户登录到站点,并将用户引导到带有一些链接的页面。

<?php
session_start();
$username = $_POST ['username'];
$password = $_POST ['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost", "db12345", "db12345") or die("could not connect to mysql"); 
mysql_select_db("db12345") or die("could not connect to db");
}
$query = mysql_query("SELECT * FROM members WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while  ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ($username==$dbusername&&$password==$dbpassword)
{
echo ("you're in, click <a href="#">here</a> to enter your profile");
$_SESSION['username']=$username; 
}
else
{
echo ("please enter your username and password");
}
?>

谢谢。我肯定这只是我搞砸了一些非常简单的东西!

您需要转义这一行中的双引号:

echo ("you're in, click <a href="#">here</a> to enter your profile");

应该是这样的:

echo ("you're in, click <a href='"#'">here</a> to enter your profile");

您还缺少第35行的右括号}

在第30行以上的回显中,您在双引号字符串中使用了双引号。

应该是这样的:

echo ("you're in, click <a href='"#'">here</a> to enter your profile");

上面一行就是问题所在。在使用双引号作为包围分隔符的字符串中使用双引号。要么使用单引号换行(并转义里面的单引号),要么使用双引号(并转义里面的双引号)。例如

// double quotes with escaped double quotes:
echo ("you're in, click <a href='"#'">here</a> to enter your profile");
// single quotes, with escaped single quote
echo ('you''re in, click <a href="#">here</a> to enter your profile');

对不起,你需要用正斜杠转义代码中的" '

将代码中的文本替换为:

echo ("you're in, click <a href='"#'">here</a> to enter your profile");