I’m continuing to write a maze generator to teach myself php…
I’ve set up a session, and a form to collect a name and a maze size (5-20).
On the first run, that works fine, the maze is generated.
What I would like for further sessions is that the name is remembered, but the size is re-input.
I’ve tried using another (different) form if the session is not new, but it’s not recognising/running it, and errors, telling me that $mazesize is an unrecognised index.
Code:
<?php
session_start();
error_reporting(-1);
?>
<html>
<head>
</head>
<body>
<?php
include 'ClassFunc1.php';
//program functionality
if (!isset($_SESSION['name']) && !isset($_POST['name'])){
//if no data, print form
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Name: </td><td><input type="text" name="name"></td></tr>
<tr><td>Maze size (min 5 max 20): </td><td><input type="text" name="mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
}else if (!isset($_SESSION['name']) && isset($_POST['name']) && isset($_POST['mazesize'])){
//if a session doesn't exist but the form has been submitted
//check to see if the form has all required values
//create a new session
if(!empty($_POST['name'])){
$_SESSION['name']=$_POST['name'];
$_SESSION['start']=time();
echo "Welcome, ".$_POST['name'].". A new session has been activated for you. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.";
if($_POST['mazesize']>20){
$m_size=20;
}elseif($_POST['mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['mazesize'];
}
define("COL_MAX",$m_size);
define("ROW_MAX",$m_size);
run_maze($_POST['name']);
close_down();
}else{
echo "ERROR. Please enter your name and a maze size";
}
}else if (isset($_SESSION['name'])) {
//if a previous session exists
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
//Ask for a new maze size
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Maze size (min 5 max 20): </td><td><input type="text" name="mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
if($_POST['mazesize']>20){
$m_size=20;
}elseif($_POST['mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['mazesize'];
}
define("COL_MAX",$m_size);
define("ROW_MAX",$m_size);
run_maze($_POST['name']);
close_down();
}
//session_destroy();
?>
The “include” file is the class, methods & functions.
I’m sure I’ve done something wrong, can anyone help?
Many thanks
ETA:
(code amended after the echo “ERROR… line)
}else if (isset($_SESSION['name']) && !isset($_POST['n_mazesize'])) {
//if a previous session exists but n_mazesize not set
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
//Ask for a new maze size
?><table><tr>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<td>Maze size (min 5 max 20): </td><td><input type="text" name="n_mazesize"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td></tr>
</form></table>
<?php
}else if (!isset($_SESSION['name']) && isset($_POST['n_mazesize'])){
//if a previous session exists and n_mazesize is set
//calculate elapsed time since session start and now
echo "Welcome back, ".$_SESSION['name'].". This session was activated ".round((time()-$_SESSION['start'])/60)." minute(s) ago. Click <a href=".$_SERVER['PHP_SELF'].">here</a> to refresh the page.<br />";
echo "Current session ID: ".session_id();
if(!empty($_POST['n_mazesize'])){
if($_POST['n_mazesize']>20){
$m_size=20;
}elseif($_POST['n_mazesize']<5){
$m_size=5;
}else{
$m_size=$_POST['n_mazesize'];
}
run_maze($_SESSION['name'], $m_size);
close_down();
}
//session_destroy();
}
?>
</body>
</html>
This asks for the size but on submission returns a blank screen 🙁
The value of $_POST[‘mazesize’] is only available when the form is submitted. You should add a condition: