dongyanju1094 2014-01-02 08:43
浏览 38
已采纳

PHP中的页面刷新会破坏会话

I am developing a web application using PHP in which a user initially has to sign in and view his content. I am using PHP sessions to maintain state. I encountered following problems:

  1. Although I started the session on each page and after relevent session variables are set, the session is destroyed each time the page is refreshed or when I browse the same URL on a different tab.
  2. I need the user to be redirected to his content page when the user browsed login page with he has already logged in.

I'm really new to PHP, So I have no idea how to solve these problems. I referred several questions in the stackoverflow, but they all say that sessions are not destroyed on page refresh. I could not understand what's wrong with my page. Any solution with explaination is greatly appreciated.

Login page

<?php

session_start();

class Sessions{
        public static function setSessionState($userdata){
            unset($userdata['password']);
            unset($userdata['timestamp']);
            $_SESSION['user']=$userdata;
        }
    }

if(isset($_POST['username']) && isset($_POST['password'])){
        $dbcon = new DBConnection();
        $dbcon->connect();
        $username= strip_tags(stripslashes(trim($_POST['username'])));
        $password = strip_tags(stripcslashes($_POST['password']));
        echo "<script>alert($username);</script>";
        $result = $dbcon->getUser($username,$password);
        if(mysqli_num_rows($result)==1){
            $user = $dbcon->getUserData($result);     #getUserData function accepts mysqli result as an input and returns a row(array) of user details.
            if(isset($user)){
                Sessions::setSessionState($user);
                header("location:index.php");
            }
            else{
                echo "user variable is not set!!!";
            }
        }
        else if(mysqli_num_rows($result)==0){
            echo "Login error! Username or Password incorrect!";
        }
        else{
            die("Unknown Error occured!");
        }
    }
............

Index page(in which user's private content is visible)

<?php 

    session_start();

    if(isset($_SESSION['user'])){
        print_r($_SESSION['user']);
    }
    else{
        echo "session variable not set";
    }
?>

Thank you.

  • 写回答

2条回答 默认 最新

  • dsh12544 2014-01-03 05:02
    关注

    I finally found the answer which is actually my bad. I didn't mention the last part of the index.php file as I though that part is irrelevant.In that part I have a part,

    <form action="<?php session_destroy(); ?>">
    

    After commenting that session_destroy() method call, I could solve my problem and keep session alive.

    Sorry for incomplete code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题