这里是要实现安全登录的登录系统/
main_login.php
<form name="form1" method="post" action="checklogin.php">
Username:<input name="myusername" type="text" id="myusername" /> <br />
Password:<input name="mypassword" type="password" id="mypassword" />
<input type="submit" name="Submit" value="Login" />
</form>
检查登录.php
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="cosmos"; // Database name
$tbl_name="members"; // 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");
// Define $myusername and $mypassword
$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 username='$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:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
登录成功.php
<?php
session_start();
if(isset($_SESSION['username']) && ($_SESSION['username'] == $myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful. <a href="logout.php">Logout</a>
</body>
</html>
注销.php
<?php
session_destroy();
header("location:main_login.php");
?>
问题是我想通过密码加密或任何其他方法(如果有的话)进行安全登录。我是 PHP 初学者
最佳答案
您可以使用 md5 对密码进行一定程度的加密。您需要对用户注册时和登录 md5 之前的密码进行 md5....
例子:
//定义 $myusername 和 $mypassword
$我的用户名=$_POST['我的用户名'];
$mypassword=$_POST['mypassword'];
$我的密码 = md5($我的密码);
每当有用户注册时,您也需要使用它。
关于PHP Secure Login - 密码加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3590193/