如果用户未登录,则回显登录脚本

时间:2014-03-31 11:28:36

标签: php

我在使用此代码时收到语法错误,我想要的是登录脚本仅在用户未登录但在第4行的第一行开始时收到错误时显示:

<?php
if($_SESSION['loggedin'] != TRUE){

echo "Already a member?&nbsp;
<form id=\"loginform\" method=\"post\" action="$_SERVER['PHP_SELF'];"
<label for=\"username\"> Email Address:</label>
<input type=\"text\" name=\"liusername\"/>
<br>
<label for=\"password\"> Password:</label>
<input type=\"text\" name=\"lipassword\" id=\"password\" />
</div>
<div class=\"submit\">
<input name=\"lisubmit\" type=\"submit\" value=\"submit\">
</div>\n";

}else{
 echo 
"<a href = logout.php>Log out</A>\n";
?>
}

3 个答案:

答案 0 :(得分:1)

将表格开头部分更改为以下内容。这应该使代码工作:

<form id=\"loginform\" method=\"post\" action='".$_SERVER['PHP_SELF']."'>

取出'?&gt;'超出其他{}。如果它没有用,请告诉我。

答案 1 :(得分:0)

您的echo不正确。

试试这段代码:

<?php
if($_SESSION['loggedin'] != TRUE){

echo "Already a member?&nbsp;
<form id=\"loginform\" method=\"post\" action=".$_SERVER['PHP_SELF'];"
<label for=\"username\"> Email Address:</label>
<input type=\"text\" name=\"liusername\"/>
<br>
<label for=\"password\"> Password:</label>
<input type=\"text\" name=\"lipassword\" id=\"password\" />
</div>
<div class=\"submit\">
<input name=\"lisubmit\" type=\"submit\" value=\"submit\">
</div>\n";

}else{
 echo 
"<a href = logout.php>Log out</A>\n";
?>
}

答案 2 :(得分:0)

关闭括号不在PHP上下文中

}else{
echo "<a href = logout.php>Log out</A>\n";
?>
}

应该是

    echo "<a href = logout.php>Log out</A>\n";
}
?>

另外,已经提到过,错误的变量连接

"$_SERVER['PHP_SELF'];"

必须是

" . $_SERVER['PHP_SELF'] . "

会话未开始

session_start(); // Before http body, before $_SESSION usage

除此之外,您不应将(PHP)逻辑和(HTML)标记放在同一文件/上下文中。