我已經adpopted一些代碼來創建一個登錄,checklogin,成功的登錄,失敗的登錄和註銷頁面。 checklogin頁面主要檢查從登錄發佈的用戶名和密碼。如果這些都是正確的,最終會登錄成功登錄頁面。不過,我想在成功的登錄頁面上說'Welcome John',但不知道如何從會話中獲取用戶名,以便我可以在此基礎上查詢以撤回登錄的用戶名。該checklogin頁:會話用戶ID?
<?php require_once('Connections/Connection1.php'); ?>
<?php
//$host="localhost"; // Host name
//$username=""; // Mysql username
//$password=""; // Mysql password
//$db_name=""; // 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");
mysql_select_db($database_Connection1, $Connection1);
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=md5($_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 userid='$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("myusername");
session_register("mypassword");
header("location:main.php");
}
else {
header("location:login_failed.php");
//echo "Wrong Username or Password";
}
?>
然後我成功的登錄頁面上,我怎麼迴應的用戶名?或者至少在查詢中引用它,然後撤回附加信息,如名稱?
非常感謝!
看到這些教程,它可以幫助你http://thenewboston.org/watch.php?cat=11&number=136 – Ligth
我相信你需要'session_start();'在所有頁面中,然後使用'if(isset ... $ _ POST)'和echo'$ myusername'。沿着這些線的東西。 –
如果我的用戶名是「'或1 = 1;從用戶刪除會發生什麼; - 」? –