2014-09-29 57 views
-1

我試圖重定向我的頁面,如果登錄爲真,也是新的會話,所以它應該像輸入詳細信息一樣工作,它應該顯示一條錯誤消息。請幫助我使用此代碼。無法使用PHP_SELF進行重定向

<?php 
if(isset($_SESSION['validate'])){ 
if(isset($_POST['Username'])){ 
$username=$_POST['Username']; 
$password=$_POST['Passwd']; 
if($username=='admin' && $password=='admin'){ 
$_SESSION['validate']=true; 
echo "logged in.."; 
header('location:127.0.0.1/php/admin.php'); 
exit(0); 
} 
else{ 
    echo "Wrong Password"; 
} 
} 
else{ 
    echo "Set fields please.."; 
} 
} 

?> 
<html> 
<head><title>Login</title></head> 
<body> 
<br><br><br><br><br><br><br><br><br> 
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> 
<?php session_start(); ?> 
<table border="0" align="center"> 
<tr> 
    <td>Username:</td> 
    <td><input type="text" name="Username"></td> 
</tr> 
<tr> 
    <td>Password:</td> 
    <td><input type="Password" name="passwd"></td> 
</tr> 
<tr> 
    <td colspan="2"><center><input type="submit" name="submit"></center></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
+0

無法打印重定向前的任何內容。 – 2014-09-29 12:34:27

+0

輸出後無法更改標題。如果你刪除「回聲」登錄..「;」在第8行,它應該工作得很好! – AndVla 2014-09-29 12:35:28

回答

1
<?php 
     if(isset($_SESSION['validate'])){ 
      echo "you are already logged in!"; 
     } 
     if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
      $username=$_POST['Username']; 
      $password=$_POST['Password']; 
      if(!empty($username) && !empty($password)){ 
      if($username=='admin' && $password=='admin'){ 
       $_SESSION['validate']=true; 
       //echo "logged in.."; //not required because after redirecting it will         stateless. 
       header('location:127.0.0.1/php/admin.php'); 
       exit(0); 
      } 
      else{ 
       echo "Wrong Credential"; 
      } 
      } 
      else{ 
      echo "Set fields please..";//it will appear if credentials are empty.. 
      } 
     } 
     ?> 
    <html> 
    <head> 
     <title>Login</title> 
    </head> 
    <body> 
     <br><br><br><br><br><br><br><br><br> 
     <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> 
    <?php session_start(); ?> 
    <table border="0" align="center"> 
     <tr> 
      <td>Username:</td> 
      <td><input type="text" name="Username"></td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="Password" name="password"></td> 
     </tr> 
     <tr> 
      <td colspan="2"><center><input type="submit" name="submit"></center></td> 
     </tr> 
     </table> 
     </form> 
    </body> 
    </html> 
+0

完成!它的工作雖然有一個小錯誤,但我糾正它!:) Thankx很多jan_sat! – m1lak0 2014-09-29 13:26:27

+0

你是受歡迎的..並感謝您選擇我的答案最好。 – 2014-09-30 05:19:57

0
<?php 
session_start(); 
if(isset($_POST['Username'])){ 
    $username=$_POST['Username']; 
    $password=$_POST['passwd']; 
    if($username == "admin" && $password=="admin"){ 
     $_SESSION['validate'] = true; 
     echo "logged in.."; 
     header('location: http://127.0.0.1/php/admin.php'); 
     exit(0); 
    } 
    else{ 
     echo "Wrong Password"; 
    } 
} 
else{ 
    echo "Set fields please.."; 
} 


?> 
<html> 
<head><title>Login</title></head> 
<body> 
<br><br><br><br><br><br><br><br><br> 
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST"> 

<table border="0" align="center"> 
<tr> 
<td>Username:</td> 
<td><input type="text" name="Username"></td> 
</tr> 
<tr> 
<td>Password:</td> 
<td><input type="Password" name="passwd"></td> 
</tr> 
<tr> 
<td colspan="2"><center><input type="submit" name="submit"></center></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 

刪除,如果(isset($ _ SESSION [ '驗證']))
原因的變量是不是在你的代碼中定義
這對我的作品

+0

在頁面上寫着「請設置字段..」,但是在點擊提交而沒有提供任何輸入而不是直接提交後,我想要它。 – m1lak0 2014-09-29 12:49:46

+0

如果沒有輸入,必須說「請設置字段..」。 :( 我真的很困惑 – m1lak0 2014-09-29 13:00:58