dongwupu5991 2015-11-04 11:18
浏览 54

使用PHP会话进行页面导航

I am trying to make simple navigation rules for a visitor for my website. I want to prevent visitor accessing specific pages if they have not visited other pages before. I have this simple code on page1.php:

<?php 
 session_start();
 $_SESSION["currentPage"] = 1;
?>

And this code on my page2.php:

<?php
 session_start();
 if ($_SESSION["currentPage"] == 1 ) {
   $_SESSION["currentPage"] = 2;
   $_SESSION["test"] = "Hello";
 }else{ 
   $_SESSION["currentPage"] = 3;
  }
?>

After visiting page1.php and then page2.php and echoing out $_SESSION["currentPage"] and $_SESSION["test"] I get "Hello" and "3".

I just don't understand why the code is executed in both blocks of the if statement...I need it to stop after $_SESSION["currentPage"] is set to "2".

I am running PHP 5.3.28

  • 写回答

1条回答 默认 最新

  • douruocai4111 2015-11-04 11:35
    关注

    It executes

    $_SESSION["currentPage"] = 2;
    $_SESSION["test"] = "Hello";
    

    When you open page2.php first time, and

    $_SESSION["currentPage"] = 3;
    

    When you refresh the page because $_SESSION["currentPage"] is not 1 anymore.
    $_SESSION is persistent across request. Clear cookies to and try again.

    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答