我已经采用了一些代码来创建登录,checklogin,成功登录,登录和登出页面失败。 checklogin页面基本上检查登录时发布的用户名和密码。如果这些都是正确的,您最终会成功登录页面。但是我想在成功的登录页面上说“欢迎约翰”之类的东西,但是不知道如何从会话中获取用户名,以便我可以根据此查询来取回登录的人名。 checklogin页面是:
<?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";
}
?>
然后在我成功的登录页面上,如何回显用户名?或至少在查询中引用它然后撤回名称等附加信息?
非常感谢!!
答案 0 :(得分:1)
稍微更改了代码,这样更安全..
<?php
session_start();
// Sanitize $_POST['myusername'] and $_POST['mypassword'] before loading into session variables to protect from MySQL injection
$_SESSION["myusername"]=!empty($_POST['myusername'])?mysql_real_escape_string(stripslashes($_POST['myusername'])):"";
$_SESSION["mypassword"]=!empty($_POST['mypassword'])?mysql_real_escape_string(stripslashes($_POST['mypassword'])):"";
// Load database variables, connect to server and select a database
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
mysql_connect($host, $username, $password)or die("Cannot Connect for Reason:".mysql_error());
mysql_select_db("$db_name")or die("Cannot Select DB for Reason:".mysql_error());
// Run query
$result=mysql_query("SELECT username FROM $tbl_name WHERE username='".$_SESSION["myusername"]."' AND password='".$_SESSION["mypassword"]."'");
// Check for return of single record and direct to login_success.php
if(mysql_num_rows($result)==1){header("location:login_success.php");}
else{
// On login falier, unset session variables if not needed and redirect
unset($_SESSION["myusername"]); // Optional if return value not needed or wanted
unset($_SESSION["mypassword"]); // Optional if return value not needed or wanted
header('refresh: 5; url=./login_fail.php');
die("Wrong Username or Password. Redirecting..."); // To prevent evil people manipulating the page, kill the script using die.
}
?>
在另一页上回显用户:echo $ _SESSION ['myusername'];
答案 1 :(得分:1)
当我需要这样做时,我通常使用php的$_SESSION
数组并在那里设置快速查找信息,例如可能在多个页面上显示的名称。
$_SESSION['username'] = $username;
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['user_id'] = $id;
这使我可以在php页面开头调用session_start()
的任何页面上访问所有这些变量。
旁注 -
使用MD5哈希密码不安全!了解如何使用crypt()
函数与盐。这将是一个很好的做法,使您的数据库更安全。不幸的是,MD5在这一点上并不比明文更安全,因为人们可以轻松访问彩虹表和ocl-hashcat等程序。
请查看使用PDO或mysqli进行数据库调用。不推荐使用mysql_
个函数。
答案 2 :(得分:0)
你有两个选择......
您可以在main.php页面上以$_SESSION['myusername'];
或者,您可以将其作为header("location:main.php?username=".$myusername);
然后在main.php上,您可以将其检索为$_GET['username'];