簡體   English   中英

如何獲得登錄頁面以返回到php主頁

[英]How get a login page to go back to the homepage in php

我有登錄代碼,用戶可以在其中登錄網站,並從數據庫中獲取詳細信息。 當用戶登錄並希望他們被帶回網站的主頁時,我似乎無法正常工作。

這是我的代碼

主要登錄

 <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Buzz Party | Login</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <?php session_start(); session_destroy(); ?> <body> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> </body> </html> 

檢查登錄

 ?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="login"; // Database name $tbl_name="users"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE name='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("sername"); session_register("password"); header("location:login.php"); } else { echo "Wrong Username or Password"; } ?> 

登錄成功

 <?php session_start(); if isset($_SESSION['myusername']) { header("Location:myindex.php"); } ?> 

header(“ location:login.php”); 如果已將任何輸出發送到瀏覽器,將無法使用。

確保在發送標題之前不向瀏覽器發送任何內容,或者可以緩沖輸出,然后在准備好之后發送所有內容,例如:

ob_start();
// do stuff
header("location:login.php");
echo ob_get_clean();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM