0

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>';
    }
maxisme
  • 3,974
  • 9
  • 47
  • 97
  • More Important !!! you need to write your session_start() on the top of your index page and put this code also ob_start(); at the beginning your page.. – VIVEK-MDU Feb 09 '14 at 06:27
  • @VIVEK-MDU `ob_start();` where? form.php or start.php? – maxisme Feb 09 '14 at 06:29
  • @user2768038.. you need to put ob_start(),session_start() in each and every page or else you just create header.php and put your session_start() and ob_start() and include into your every pages.refer this http://stackoverflow.com/questions/4401949/whats-the-use-ob-start-in-php – VIVEK-MDU Feb 09 '14 at 06:33

1 Answers1

1

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{
   }
?> 
Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51