doujuanju3076 2012-11-20 18:07 采纳率: 0%
浏览 20
已采纳

跨页面的PHP会话

have this question in mind. I know there are a lot questions like this on the internet, but none of them seem to do the trick for me :(

So I'm planning on making an adminpanel on a website. You enter by going through a login screen, when the name+pass is correct an session is made. On the other adminpages I'll test if this session is set, by this I'll know if the user can access the page or not.

Inlog.php (simplyfied)

<?php session_start(); ?> <html ...

then after lots of controls

    if ($adminpwd == $pass) {
    session_regenerate_id();
    $_SESSION["mysessionname"] = $name;
    $tijd = date('H:i:s', strtotime('+ 30 minutes'));
    $_SESSION["E4A_einde"] = $tijd;
    session_write_close();
echo "<script type='text/javascript'>window.location = 'myotheradminpage'</script>";
}

My other php pages include this script

<?php session_start();
if (!isset($_SESSION["mysessionname"])) {

session_destroy();
echo '<SCRIPT LANGUAGE="JavaScript"> window.alert("no session found") </SCRIPT>';
echo "<script type='text/javascript'>window.location = 'myloginpage'</script>";
        }

With this element, if the session is not set there's a redirect, if the session is set the other code on the php page wil be triggered.

But the fact is that Every single time i'm redirected again to my loginpage. so my session is not set :s Can't figure out why so please help me out.

I tried a lot of thins with on every page to add the session_start() methode, I tried to regenerated the session id before I set the sessions, but nothing works..

  • 写回答

2条回答 默认 最新

  • drz49609 2012-11-20 18:13
    关注

    In your initial session_start before the HTML, try setting this:

    <?php session_start();  
    $_SESSION["mysessionname"]; ?>
    <html ...
    

    This will declare your session variable before anything runs.

    Let me know if this works.

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

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图