duanlan8763 2014-02-09 06:23
浏览 39
已采纳

检查PHP会话是否存在不起作用

I am trying to find a way to check whether there is a session. And if there isn't, redirect back to the start.php page.

These are the pages I have made.

start.php

<form action="form.php" method="post">
    <p>Please enter your name to continue:</p>
    <input type="text" name="name" id="name" />
    <input type="submit" name="enter" id="enter" value="Enter" />
</form>

form.php

Above the head:

<?php session_start(); ?>

In the body:

<?php
if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
}
$name = $_SESSION['name'];
echo $name;
?>

Attempt

I have putting this above the head of the form (along with what is there now) but it just keeps me on the start.php page

<?php 
  if (!isset($_SESSION["name"]))
   {
      header("location: start.php");
   }
   else{
   }
?>

more info

So currently if there is no session and I enter form.php it will redirect me to start.php. But if there is a session it will stay on form.php.

But if I start on start.php and submit the form (creating the session and moving to form.php) it will straight away redirect me back to start.php (the same page)?

code of the two pages in full:

start.php

<?php session_start(); ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

</head>

<body>


    <form action="deletethis.php" method="post">
        <p>Please enter your name to continue:</p>
        <input type="text" name="name" id="name" />
        <input type="submit" name="enter" id="enter" value="Enter" />
    </form>


</body>
</html>

form.php

<?php 
  session_start();
  if (!isset($_SESSION["name"]))
   {
      //header("location: delete1.php");
      die('<meta http-equiv="refresh" content="0;URL='.'delete1.php'.'" />');
   }
   else{
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
}
$name = $_SESSION['name'];
echo $name;
?>
</body>
</html>

What I have decide to do instead

if (strlen($name) <1){
    echo '<script> window.location.replace("delete1.php");</script>';
    }
  • 写回答

1条回答 默认 最新

  • douyan7916 2014-02-09 06:28
    关注

    You need to add

    <?php session_start(); ?>
    

    on each page in the very beginning..

    So, the code should be like this

    <?php 
      session_start();
      if (!isset($_SESSION["name"]))
       {
          //header("location: delete1.php");
          die('<meta http-equiv="refresh" content="0;URL=\'delete1.php\'" />');
       }
       else{
       }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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