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>';
}