2014-03-31 76 views
1

我在使用此代碼時收到語法錯誤,我想要登錄腳本僅在用戶未登錄但收到錯誤時才顯示在第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"; 
?> 
} 
+1

將'session_start();'添加到頂部。 –

+0

我在頁面頂部有這個,上面有一些其他的腳本 – user3480862

+2

你在'action ='之後通過使用非轉義的'''結束了你的字符串值......一般來說,你不應該使用echo來輸出大的部分大多是靜態的HTML代碼 - 最好離開PHP解析器,直接編寫HTML代碼,並且只在必要時使用PHP。而且由於$ _SERVER ['PHP_SELF']'的未處理輸出是XSS漏洞 - 最好只是 – CBroe

回答

1

將表格開頭部分更改爲以下內容。這應該使代碼工作:

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

並從其他{}中取出'?>'。讓我知道如果它沒有幫助。

+0

這個工作原理如果您只想將表單發送到當前頁面。 ,謝謝 – user3480862

+1

@ user3480862將此答案標記爲正確並解決問題;) – Farzad

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"; 
?> 
} 
+1

使用單引號時,換行符字符(\ n)不會被解釋,您必須將它們刪除 – Gwenc37

+0

@ Gwenc37 Tnx,固定。 – Farzad

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)標記在同一個文件/上下文。