PHP $ _SESSION问题

时间:2011-07-25 15:31:34

标签: php mysql database session

我正在为我的网站创建一个登录页面。它也完成了[在某种意义上完成它,不是在它是安全的意义上。]我在第30行收到错误“$ _SESSION ['username'] = $ username;” - “解析错误:语法错误,意外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");
}

?>

谢谢。我确定这只是一件非常简单的事我搞砸了!

5 个答案:

答案 0 :(得分:4)

你需要在这一行中转义双引号:

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

答案 1 :(得分:2)

你还错过了第35行的结束括号}

答案 2 :(得分:1)

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

应该是这样的:

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

答案 3 :(得分:0)

上面的线是问题。您在使用双引号作为包围分隔符的字符串中使用双引号。要么使用单引号来包装(并在其中转义单引号),要么使用双引号(并在其中转义双引号)。 e.g。

// 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');

有关其他示例,请参阅php strings上的手册。

答案 4 :(得分:0)

抱歉,你需要使用正斜杠\

来逃避代码上的“”

将代码中的此文字替换为:

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