dsrbb20862 2016-02-01 12:53
浏览 51

php mysqli cookies登录

I dont know what to do anymore, when i login it's cool and then when i return back it logs me out but cookies arent destroyed, i want on whatever page i go to stay logged on, and yep, SUBMIT buttons i can press it because when i log in its not hidden, please help, im on the edge of losing it, improve my code if its wrong, and i know it is

<?php 

error_reporting(0);

$con = mysqli_connect("localhost","root","","samp");

if (mysqli_connect_errno())
{
    echo "Failed to connect to the database: " . mysqli_connect_error();
    die();
}

session_start();


if(!empty($_POST['username']) && !empty($_POST['password']))
{

    $userName = isset($_POST["username"]) ? $_POST["username"] : null;
    $userPass = isset($_POST["password"]) ? $_POST["password"] : null;

    $hashedPass = hash('whirlpool', $userPass);
    $query = "SELECT Ime FROM Igraci WHERE Ime = '$userName' AND Lozinka = '$hashedPass'";

    $result = mysqli_query( $con, $query);

    $row = mysqli_fetch_array($result);

    if($row)
    {
        $session = md5($userName.$hashedPass);
        mysqli_query($con, "UPDATE Igraci SET session = '$session' WHERE Ime = '$userName' AND Lozinka = '$hashedPass'");
        setcookie("login", $userName,time()+3600);
        echo "You are now logged in with hash: ".htmlspecialchars($_COOKIE["login"]). ' <a href="index.php?logout=1">logout</a>?';
        header('index.php');

    }
    else
    {
        die('Account has not been found.');
    }
}

if(isset($_GET['logout']))
{
    setcookie("login", "", time()-60);  
    exit(); # stop executing here
    header('index.php');
}

if(isset($_COOKIE["login"]) && mb_strlen(isset($_COOKIE["login"]) == '32'))
{
    $session = $con->real_escape_string($_COOKIE["login"]);
    $query = "SELECT Ime FROM Igraci WHERE session = '$session' LIMIT 1";
    $result = mysqli_query( $con, $query); $row = mysqli_fetch_array($result);

    if($row['Ime']) 
    { 
        echo "User is already logged in with username ".$row['Ime']. " and hash: ".htmlspecialchars($_COOKIE["login"]). ' <a href="index.php?logout=1"> logout</a>?';
        exit(); 
    }

}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Roleplay Factory User Control Panel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='https://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
</head>
<body>

<h1>Welcome, please login to your account.</h1>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" placeholder = "Username" name="username">
    <input type="password" placeholder = "Password" name="password">
    <input type="submit" name="login_button" value="Login">
</form> 

<div class="footer">
<p>roleplay factory &copy; 2016 all rights reserved</p>
</div>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dpkrh2444 2016-02-01 13:15
    关注

    update your code line

    header('index.php'); 
    

    to be

    header('location:index.php');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题