php-如何在登录时记住我
大家好,我在我的项目上遇到了一个大问题,我想在登录时记住我,但当我尝试使用下面的代码时,它不起作用,没有出现错误消息。请帮助我急需的东西,提前谢谢php-如何在登录时记住我,php,html,Php,Html,大家好,我在我的项目上遇到了一个大问题,我想在登录时记住我,但当我尝试使用下面的代码时,它不起作用,没有出现错误消息。请帮助我急需的东西,提前谢谢 您必须在所有html站点的顶部使用session\u start(),否则您将失去对会话及其所有存储变量的引用。首先,每当用户登录时,您需要在客户端上保存一个名为哈希(随机字符串)的cookie 与此cookie一起,您必须在数据库中的表(sessions)中创建一行,该行的值为哈希值和相应的登录用户id 例如:如果用户4登录;它将生成一个值为123
您必须在所有html站点的顶部使用session\u start()
,否则您将失去对会话及其所有存储变量的引用。首先,每当用户登录时,您需要在客户端上保存一个名为哈希
(随机字符串)的cookie
与此cookie一起,您必须在数据库中的表(sessions
)中创建一行,该行的值为哈希值和相应的登录用户id
例如:如果用户4
登录;它将生成一个值为1234
的hash
,然后该值将与该用户id(4
)一起存储在数据库表中
每当用户访问网站时,您需要检查cookie值是否已设置,如果已设置,则检查该值是否与数据库中的任何值匹配。假设它找到了一个,抓取与相应值匹配的用户id
,并将该特定用户登录
确保登录后生成一个新的散列
,并从会话
表中删除旧散列
当用户注销时也会发生同样的情况;每次从客户端和数据库表中删除散列(出于安全目的)
希望这能让你知道你需要做些什么来实现这一目标
如果您想更深入地解释如何做到这一点,我强烈建议您阅读以下内容:
SIDENOTE:我注意到您仍在使用已弃用的mysql.*
扩展。请停止使用mysql.*
,它不再安全,而且还有更好的替代方案。我建议mysqli.*
或PDO
我已经有了一个会话\u start(),如果我没有弄错的话,关闭浏览器也会破坏当前会话,这意味着您必须重新登录,否则我无法理解您的代码中没有任何会话\u start()。在保存任何变量之前,您必须在每个页面上使用session_start(),并将其作为第一个函数;在我的代码顶部,但仍然没有发生任何事情。我还没有真正了解代码的功能,但我想我可能会告诉您,您确实不应该使用旧的不推荐使用的mysql扩展。自PHP 5.5(2013年6月)以来,它一直被弃用,现在已正式从PHP 7.0(2015年12月)中删除。大多数开发人员在不推荐使用此扩展时都应该避免使用它;但是,现在您应该使用PDO或mysqli,因为PHP不再支持旧的mysql扩展。
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
mysql_connect("localhost", "root", "");
mysql_select_db("vrooms");
$result = mysql_query("SELECT * FROM registration where username = '$username' and password = '$password'")
or die("Failed to query database" .mysql_error());
$row = mysql_fetch_array($result);
if($username != $username){
header("location: home/homepage.php");
echo'<script>
alert("Not Allowed to Login With a Different Account!");
</script>';
$username = $_POST['username'];
}
if($row['username'] == 'admin_jake' && $row['password'] == $_POST['password']){
$_SESSION['type'] = 'admin';
}
if($row['username'] == $_POST['username'] && $row['password'] == $_POST['password'] && $_SESSION['type'] != 'admin'){
$_SESSION['type'] = 'user';
$_SESSION['username'] = $username;
header("location: home/homepage.php");
}
else if($row['username'] != $_POST['username'] && $row['password'] != $_POST['password']){
$_SESSION['message'] = "Incorrect Username or Password";
header("location: loginpage.php");
}
else if($_SESSION['type'] == 'admin' && $_SESSION['type'] != 'user'){
$_SESSION['admin'] = $username;
header("location: admin/adminpage.php");
}
if(isset($_REQUEST['remember']))
$escapedRemember = myqli_real_escape_string($conn, $_REQUEST['remember']);
$cookie_time = 60 * 60 * 24 * 30;
$cookie_time_Onset = $cookie_time + time();
if(isset($escapedRemember)){
setcookie("username", $username, $cookie_time_Onset);
setcookie("escapedPW", $password, $cookie_time_Onset);
}
else{
$cookie_time_fromOffset = time() - $cookie_time;
setcookie("username", '', $cookie_time_fromOffset);
setcookie("password", '', $cookie_time_fromOffset);
}
?>
<?php
session_start();
include_once("CORE/dbconfig.php");
if(isset($_SESSION['type'])){
if ($_SESSION['type'] == 'user') {
header("location: home/homepage.php");
}
else if ($_SESSION['type'] == 'admin') {
header("location: admin/adminpage.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Car Hub - Don't dream, ride it!</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="CSS FILES/login_chstyle.css">
</head>
<body>
<br>
<?php
if(isset($_SESSION['message'])){
echo '<div class = "msg">';
echo '<p>' .$_SESSION['message']. '</p>';
unset($_SESSION['message']);
echo '</div>';
}
?>
<div class="header">
<a href="loginpage.php"><img src="images/CarHubLogos.png" style="margin-top: 10px; height: 50px"></a>
<!-- ___________________________________________________________________________For Log In Syntax_______________________________________________________________ -->
<div id="buttonsize"><button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Sign In</button></div>
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal" style="margin-top: 50px">×</span>
<form class="modal-content animate" action="login.php" method="POST">
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" id="username" value="<?php if(isset($_COOKIE['username'])) echo $_COOKIE['username']; ?>" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" id="password" value="<?php if(isset($_COOKIE['password'])) echo $_COOKIE['password']; ?>" required>
<input type ="checkbox" id="remember" name="remember" <?php if(isset($_COOKIE['username'])){ echo"checked = 'checked'";}?> value="1">
<label>Remember Me</label>
<button class="colorgr" name="login" type="submit">Login</button>
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
<!-- ___________________________________________________________________________For Log In Syntax_______________________________________________________________ -->
<!--____________________________________________________________________________SCRIPT START ___________________________________________________________________ -->
<script>
// Get the modal for Log In
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
// Get the modal for sign up
var modal = document.getElementById('id02');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<!--____________________________________________________________________________SCRIPT END ____________________________________________________________________ -->
<!--____________________________________________________________________________Sign Up ____________________________________________________________________ -->
<div id="buttonResize">
<button onclick="document.getElementById('id02').style.display='block'" style="width:auto;">Sign Up</button>
</div>
<div id="id02" class="modal">
<span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal" style="margin-top: 50px">×</span>
<form name="myForm" class="modal-content animate" action="signup.php" method="post">
<div class="container">
<label><b>Last Name</b></label><br>
<input type="text" placeholder="Enter Last Name" id="customer_lname" name="customer_lname" pattern="[a-zA-Z ]+" title="Must not contain a special character and numbers. e.g. !@#$%^&*0-9" required>
<br>
<label><b>First Name</b></label><br>
<input type="text" placeholder="Enter First Name" id="customer_fname" name="customer_fname" pattern="[a-zA-Z ]+" title="Must not contain a special character and numbers. e.g. !@#$%^&*0-9" required>
<br>
<label><b>Contact Number </b></label><br>
<input type="tel" placeholder="Enter Contact Number" id="contact_number" name="contact_number" pattern="^\d{4}-\d{3}-\d{4}$" title="XXXX-XXX-XXXX" style = "width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;" required>
<br>
<label><b>Email</b></label><br>
<input type="email" placeholder="Enter Email" id="email_address" name="email_address" style = "width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;" required>
<br>
<label><b>Username</b></label><br>
<input type="text" placeholder="Enter Username" id="username" name="username" pattern="^[a-z0-9_-]{4,16}$"
title="Must contain at least 4-16 characters and must not have some special character. e.g !@#$%^&*" required >
<br>
<label><b>Password</b></label><br>
<input type="password" placeholder="Enter Password" id="password" name="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" onchange="form.c_password.pattern = this.value;" required >
<br>
<label><b>Repeat Password</b></label><br>
<input type="password" placeholder="Repeat Password" id="c_password" name="c_password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Password Must Match!" required>
<p>By creating an account you agree to our <a href="terms.php" style="color: blue;">Terms & Privacy</a>.</p>
<div class="clearfix">
<button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
<button class="colorgr" type="submit" name="submit_cus" class="signupbtn">Sign Up</button>
</div>
</div>
</form>
</div>
<!--____________________________________________________________________________Sign Up ____________________________________________________________________ -->
</div>
<br>
<hr>
<br>
<ul>
<li><a href="loginpage.php">Home</a></li>
<li><a href="vehicle.php">Vehicles</a></li>
<li><a href="aboutus.php">About</a></li>
<li><a href="faq.php">FAQ</a></li>
</ul>
<div id="bodywall">
<br>
<h1 class="gety">Ride a<br>car today</h1>
<p class="stylo1">Sign up for free</p>
<br><br><br><br><br><br><br><br><br><br>
</div>
<div class="footer"><img src="images/CarHubLogos.png" style="height: 100%"></div>
</body>
</html>
<script>
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
};
} else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
} else {
ignoreHashChange = false;
}
};
}
}
</script>