PHP:在页面重新加载后,不会保存cookie中的会话信息? [英] PHP: Setting the session information in a cookie won't be kept after page reloads?

查看:118
本文介绍了PHP:在页面重新加载后,不会保存cookie中的会话信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的php单页面,需要用户插入一个特定的用户名和传递为了访问它的内容。
它生成一个cookie,允许用户访问该页面一天。
如果用户已登录,则显示内容列表。



这是一个单一的index.php页面。

这个单一的受保护页面包含一个表单,用户可以在其中输入一些信息并保存。用户登录后,所有内容都按预期显示。但是,当用户尝试提交表单并重新加载网页(新内容应添加到该网页)时,它会被踢出,并且表单中包含的信息会丢失,并且不会保存。 p>

这是index.php页面的特定部分:

 < ;?php session_start(); ?> 
<!DOCTYPE html>
[...]

<?php
if(isset($ _ POST ['loguearse'])){
$ _SESSION ['user'] = strip_tags($ _ POST ['user']);
$ _SESSION ['pass'] = strip_tags($ _ POST ['pass']);
if($ _ SESSION ['user'] =='myuser'&& $ _SESSION ['pass'] =='mypass'){
if(isset($ _ SESSION ['user' ])){
session_start();
setcookie(usuario,$ _ POST ['user'],time()+ 24 * 60 * 60);
setcookie(clave,$ _ POST ['pass'],time()+ 24 * 60 * 60);
}

[这里的内容登录控制]

 } 
} else {
setcookie(usuario, );
setcookie(clave,);

echo'
< form method =post>
< div class =form-group>
< input type =textclass =form-controlname =userid =userplaceholder =Usuario>
< / div>
< div class =form-group>
< input type =passwordclass =form-controlname =passid =passplaceholder =clave>
< / div>
< / div>
< div class =modal-footer>
< input type =submitname =loguearseclass =btn btn-primary>
< / div>
< / div>
< / form>
';
echo'没有puedes entrar罪恶poner拉clave correcta!
}
?>

我的问题是:如何保持该用户登录并使用活动会话24小时?

解决方案

您的测试订单是这里的问题。您最初测试的是POST变量,而不是SESSION变量。请尝试以下方法:


  1. 测试注销以查看用户是否尝试注销。


  2. 测试会话变量以表明他们已经登录。

    b
  3. 如果 1和2为假,请测试登录。如果是,请初始化会话。



I have a very simple php single page, that requires the user to insert a specific username and pass in order to access its contents. It generates a cookie that allows the user to access that page for one day. If the user is logged in, the list of contents appear. If it's not, it shows the form.

It is all inside a single index.php page.

This single "protected" page contains a form where the user can put some information and save it. After the user logs in, all the content is shown as intended. But when the user tries to submit that form and reloads the page (the new content should be added to that page), it gets kicked out and the information contained in the form gets lost, and it's not saved.

This are the specific parts of the index.php page:

<?php session_start(); ?>
<!DOCTYPE html>
[...]

<?php 
if(isset($_POST['loguearse'])) { 
  $_SESSION['user']=strip_tags($_POST['user']);
  $_SESSION['pass']=strip_tags($_POST['pass']);
    if($_SESSION['user'] == 'myuser' && $_SESSION['pass'] == 'mypass') {
      if (isset($_SESSION['user'])) {
      session_start();
                setcookie ("usuario",$_POST['user'], time()+24*60*60);
                setcookie ("clave",$_POST['pass'], time()+24*60*60);
      }

[HERE IT GOES THE CONTENT THAT WORKS OK IF I STRIP THE LOGIN CONTROL]

  }
    } else { 
                    setcookie("usuario","");
                    setcookie("clave","");

      echo '
            <form method="post">
              <div class="form-group">
                <input type="text" class="form-control" name="user" id="user" placeholder="Usuario">
              </div>
              <div class="form-group">
                <input type="password" class="form-control" name="pass" id="pass" placeholder="clave">
              </div>
              </div>
              <div class="modal-footer">
                <input type="submit" name="loguearse" class="btn btn-primary">
              </div>
            </div>
            </form>
      ';
      echo 'No puedes entrar sin poner la clave correcta!';
    } 
?>

My question is: How do I keep that user logged in and with an active session for 24 hours?

解决方案

Your testing order is the problem here. You are originally testing for the POST variable, not the SESSION variable. Try this:

  1. Test for logout to see if the user tried to logout. If so, delete the session.

  2. Test for the session variables to indicate they're already logged in.

  3. IF 1 and 2 are false, test for login. If so, initialize session.

这篇关于PHP:在页面重新加载后,不会保存cookie中的会话信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆