2013-11-05 151 views
0

此代碼不完整,我需要它重定向到一個名爲mainpage.php的頁面,其中包含用戶名和密碼的硬編碼信息。這是代碼。PHP登錄頁面重定向

<?php 
session_start(); 
$username="user"; 
$password="123"; 

if($_POST['username'] == $username && $_POST['password'] == $password) 
?> 

<html> 
    <head> 
    </head> 
    <body> 
     <div style='text-align:center'> 
     <h3>Welcome To Kontak</h3> 
     </div> 
     <hr /><br /> 
     <form id='login' action="" method='post' accept-charset='UTF-8'> 
      <fieldset style="width:550px"> 
      <legend>Admin Login</legend> 
      <input type='hidden' name='submitted' id='submitted' value='1'/> 

      <label for='username' >UserName:</label> 
      <input type='text' name='username' id='username' maxlength="50" /> 


      <label for='password' >Password:</label> 
      <input type='password' name='password' id='password' maxlength="50" /> 

      <input type='submit' name='submit' value='Submit' /> 
      </fieldset> 
     </form> 
    </body> 
</html> 
+0

你到底想要做什麼?不管你說什麼這是不夠的.. – Kalpit

回答

0

不得不開始和結束,如果塊

<?php 
session_start(); 
$username="user"; 
$password="123"; 

if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password) 
{ 
    header("Location: your_page.php"); 
} 
else 
{ 
?> 

<html> 
    <head> 
    </head> 
    <body> 
     <div style='text-align:center'> 
     <h3>Welcome To Kontak</h3> 
     </div> 
     <hr /><br /> 
     <form id='login' action="" method='post' accept-charset='UTF-8'> 
      <fieldset style="width:550px"> 
      <legend>Admin Login</legend> 
      <input type='hidden' name='submitted' id='submitted' value='1'/> 

      <label for='username' >UserName:</label> 
      <input type='text' name='username' id='username' maxlength="50" /> 


      <label for='password' >Password:</label> 
      <input type='password' name='password' id='password' maxlength="50" /> 

      <input type='submit' name='submit' value='Submit' /> 
      </fieldset> 
     </form> 
    </body> 
</html> 
<?php 
} 
?> 
+0

yup複製該語句,以避免再次打字相同,但是在我的編輯副本這個答案中添加 – Anto

+0

我試過了代碼,它的工作原理,但現在它留給我一個未定義的索引:用戶名 – user2896254

+0

對不起isset($ _ POST)isset($ _ POST ['username']) – Anto

4

我認爲你正在尋找這樣的:

if($_POST['username'] == $username && $_POST['password'] == $password) 

header('Location: mainpage.php'); 
?> 

所以頭會採取mainpage.php如果條件爲:滿意。 --Vijay

+0

PHP有一個verify_pasword函數或字符串比較。使用上面的http://php.net/manual/en/function.password-verify.php更高效 – Asuquo12

0

更改'header(「Location:your_page.php」);''到'header(「Location:mainpage.php」);''