dongtang6681 2017-03-13 03:43
浏览 62

php - 如何在登录时记住我[关闭]

Hi guys im having a big problem on my project i wan to have a remember me on my log in but when i try it using this code below its not working there are no error messages came out. please help me badly needed thanks in advance. here is my html code for login.

<?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">&times;</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>
  • 写回答

2条回答 默认 最新

  • dousu5608 2017-03-13 03:48
    关注

    you have to use session_start() at the top of all your html sites, otherwise you loose reference to the session and to all its stored variables.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法