简体   繁体   English

我无法在本地主机的php文件中进行会话。 即使我创建了会话,我仍被重定向

[英]I am unable to make a session in my php file in localhost. I am redirected even though i created a session

This is my code for login.html 这是我的login.html代码

<html>
<head>
<title>User Logon</title>
</head>
<body>
  <h2>User Login </h2>
  <form name="login" method="post" action="login.php">
   Username: <input type="text" name="username"><br>
   Password: <input type="password" name="password"><br>
   Remember Me: <input type="checkbox" name="rememberme" value="1"><br>
   <input type="submit" name="submit" value="Login!">
  </form>
</body>
</html>

This is login.php 这是login.php

<?php
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';

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

    if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {    

        if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie('username', $_POST['username'], time()+60*60*24*365);
            setcookie('password', md5($_POST['password']), time()+60*60*24*365);

        } else {
            /* Cookie expires in an hour */
            setcookie('username', $_POST['username'],time()+60*60);
            setcookie('password', md5($_POST['password']),time()+60*60););
        }
        header('Location: index.php');

    } else {
        echo 'Username/Password Invalid';
    }

} else {
    echo 'You must supply a username and password.';
}
?>

and this is index.php 这是index.php

<?php
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';

if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {

    if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {    
        header('Location: login.html');
    } else {
        echo 'Welcome back ' . $_COOKIE['username'];
    }

} else {
    header('Location: login.html');
}
?>

Now,index files checks for cookie and if unavailable, redirects to login.html 现在,索引文件会检查Cookie,如果不可用,则重定向到login.html

login.html takes values, passes to login.php which creates cookies. login.html会获取值,并传递给login.php来创建cookie。

But no matter how many times i do it, the output is same, i am not able to view index file because there is no cookie.. any help? 但是无论我做多少次,输出都是一样的,因为没有cookie,所以我无法查看索引文件。

You need to change this section: 您需要更改此部分:

if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {    
    header('Location: login.html');
} else {
    echo 'Welcome back ' . $_COOKIE['username'];
}

To: 至:

if (($_COOKIE['username'] != $user) || ($_COOKIE['password'] != md5($pass))) {    
    header('Location: login.html');
} else {
    echo 'Welcome back ' . $_COOKIE['username'];
}

Note that $_POST changed to $_COOKIE 请注意, $_POST更改为$_COOKIE

Check your index.php file. 检查您的index.php文件。 It has error 它有错误

<?php
/* These are our valid username and passwords */
$user = 'user';
$pass = 'user';

if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
//var_dump($_COOKIE['username']); exit;
    if (($_COOKIE['username'] != $user) || ($_COOKIE['password'] != md5($pass))) {    
        header('Location: login.html');
    } else {
        echo 'Welcome back ' . $_COOKIE['username'];
    }

} else {
    header('Location: login.html');
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我已登录,URL也已重定向-PHP /会话 - URL Redirected even though I am logged in - PHP / Session 警告:session_start():即使我正在检查现有会话,也无法发送会话缓存限制器 - Warning: session_start(): Cannot send session cache limiter, even though I am checking for existing session 为什么我会被重定向到此联系表单中的 php 文件? - Why am I being redirected to my php file in this contact form? 我正在我的本地主机上使用 Mailgun 编写教程。 发送给我一个错误 - I am working on a tutorial with Mailgun in my localhost. Send gives me an error 我是否以正确的方式使用php会话? - Am I using the php session the right way? 我在本地主机中获取文件树 - I am getting file tree in my localhost 我是否正确使用会话? - Am i using session correctly? 为什么即使在销毁上一个会话之后,我仍会在codeiginitor中获取当前会话的上一个会话数据 - Why i am getting previous session data for current session in codeiginitor even after destroying previous session PHP错误:即使传递变量,也缺少参数1 - PHP error: Missing argument 1, even though I am passing a variable 用php创建了一个登录和注销会话,但是我在注销时遇到了麻烦 - Created a Log In and Log out Session with php but i am having trouble with the Log out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM