duanlan8763 2014-02-08 22: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-08 22: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{
       }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部