Jump to content

Session interrupting other input form.


fishbaitfood

Recommended Posts

So, I have two forms..

 

One for displaying a submitted name.

 

And one for displaying an array of submitted words.

 

 

At the very top of my index.php, I have:

 

<?php session_start();

 

 

 

And only in my Second form, for displaying array of words, I've put a 'clear' button to clear the array, which works :

 

elseif (isset($_POST['reset'])) {
$_SESSION['words'] = '';
unset($_SESSION['words']);
session_destroy();
}

 

 

 

Problem:

 

When I've submitted a name in the First form, and then submit a word in the Second form, the name of the First form disappears!

 

This is also happening the other way, as well as using the reset button for the Second form.

 

 

Should I use Two Sessions? Is this possible? And how do I do it?  :confused:

 

 

Thanks.

 

Link to comment
Share on other sites

Thanks both,

 

And yes I'm trying to use Two Sessions, but If I have already one at the top, I get an error while using a second one somewhere else.

 

If I use session_unregister(); ,, I can clear the array, but then I get a Warning (Wrong parameter count for session_unregister()).

When I use session_unregister($_SESSION['words']); ,, I get a Notice (Undefined index: words).

 

Edit: this is maybe because I've used  unset already? No? I'm gonna trial and error a bit..

 

 

So the only way I can solve this, is to use Two Sessions? But If I put another session_start() I get a warning...  :shrug:

 

 

UPDATE:

 

Ok, I got session_unregister() working now:

 

elseif (isset($_POST['reset'])) {
$_SESSION['words'] = '';
session_unregister($_SESSION['words']);
}

 

But this doesn't solve my problem, listed in my first post!  :-[

 

Link to comment
Share on other sites

Look, here's my whole index.php (the two last forms is what my problem is about) :

 

<?php
session_start();
header('Content-type: text/html; charset=UTF-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

	<title>php date + javascript time + google search</title>

	<script type="text/javascript">
<!--
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var a;
m = checkTime(m);
s = checkTime(s);

if(h > 11){
	a = "PM";
} 
else {
	a = "AM";
}

if(h < 10){
	h = "0" + h;
}

document.getElementById('time').innerHTML = " " + h + ":" + m + ":" + s + "  " + a;
t = setTimeout('startTime()', 500);
}

function checkTime(i) {
if (i<10) {				// add a zero in front of numbers < 10
	i="0" + i;
}
return i;
}
//-->
	</script>

	<style type="text/css">
<!--
* {
margin: 0px auto;
padding: 0px;
}
body {
margin: auto;
font-family: Tahoma, sans-serif;
color: #098;
}
div#container {
margin: auto;
margin-top: 5px;
padding: 10px;
width: 40%;
border: 1px solid #000;
}
div#container:after {
content: ".";
display: block;
clear: both;
height: 0px;
visibility: hidden;
}
p.phptext {
float: left;
border: none;
}
span.letter {
letter-spacing: 1px;
}
div#time {
float: left;
clear: right;
}
p.google {
float: left;
clear: both;
margin-top: 10px;
border: none;
}
input.text {
margin-top: 5px;
margin-bottom: 5px;
}
span.bold {font-weight: bold; letter-spacing: 1px;}
span.blue {color: #4671E4;}
span.red {color: #E42232;}
span.yellow {color: #FFBB1C;}
span.green {color: #019311;}

form#searchForm {
float: left;
clear: right;
margin-top: 5px;
}
input.text {
width: 248px;
}
span.fsize {
font-size: 10.75pt;
}
form#login {
float: left;
clear: both;
margin: 10px 10px 0 0;
}
table#table {
font-size: 10.75pt;
color: #444;
}
h2.HelloTitle {
color: #098;
}
input.lbl {
margin: 5px 0 5px 0;
}
input.btn {
font-family: Tahoma, "MS Sans Serif", Geneva, sans-serif;
font-size: 9.25pt;
width: 75px;
height: 23px;
margin: 5px 0 10px 0;
}
div#arrays {
float: left;
clear: both;
margin-top: 20px;
}
form#arrayForm {
margin: 10px 0 0 0;
}
div.hr {
float: left;
clear: both;
margin: 20px 0 10px 0;
width: 100%;
}
-->
	</style>	
</head>

<body onload="startTime();">

	<div id="container">

		<p class="phptext">Today we are <span class="letter">
			<?php
				setlocale(LC_ALL, 'en_gbr', 'english_england');
				$weekday = (string) strftime("%A");
				$day = (int) strftime("%d");
				$month = (string) strftime("%B");
				$year = (int) strftime("%Y");

				$a = (string) "";

				if ($day==1){
					$a = "st of ";
				} elseif ($day==2){
					$a = "nd of ";
				} elseif ($day==3){
					$a = "rd of ";
				}
				else $a = "th of ";

				echo $weekday . ", " . $day . $a . $month . " " . $year . ".";
			?>
			</span>           
		</p>

		<div id="time"></div>

		<div class="hr"><hr /></div>

		<script type="text/javascript">
		function btnText() {
			var box = document.getElementById('nlBox');
			var btnText = document.getElementById('btnSearch');
			var form = document.getElementById('searchForm');
			var div = document.getElementById('divHidden');
			var txt = document.getElementById('search');

			if (box.checked) {
				btnText.value = "zoeken";
				form.action = "http://www.google.nl/search";
				div.innerHTML = '<input id="hidden1" name="hl" type="hidden" value="nl" /><input id="hidden2" name="lr" type="hidden" value="lang_nl" />';
				txt.innerHTML = "Zoek";
			}
			else {
				btnText.value = "search";
				form.action = "http://www.google.com/search";
				txt.innerHTML = "Search";
			}
		}
		</script>

		<form id="searchForm" action="http://www.google.com/search" method="get">
			<p class="google"><span id="search">Search</span> <span class="bold"><span class="blue">G</span><span class="red">o</span><span class="yellow">o</span><span class="blue">g</span><span class="green">l</span><span class="red">e</span></span>:<br/>						
				<input class="text" name="q" type="text" /><br />
				<input id="nlBox" name="nlBox" class="nlBox" type="checkbox" onclick="btnText();" /> <span class="fsize">Only show Dutch results</span><br />
				<div id="divHidden"></div>
				<input id="btnSearch" class="btn" type="submit" value="search" onclick="btnText();" />
			</p>
		</form>

		<div class="hr"><hr /></div>

		<?php
			if(isset($_POST['name'])){
				$name = $_POST['name'];
				$name = strip_tags($name);
				$name = trim($name);
				$name = htmlentities($name);

				if (sanitize($name)) {
					$_SESSION['name'][] = $name;
				}
			}
			else {
				$name = "";
			}
		?>

		<form id="login" action="index.php" method="post">
			<table id="table">
				<tr><td><h2 class="HelloTitle">Hello <?php echo $name; ?></h2></td></tr>
				<tr><td> </td></tr>
				<tr><td><label for="name">Name:</label><br /><br />
						<input id="name" name="name" type="text" value="<?php echo $name; ?>" maxlength="34" /></td></tr>
				<tr><td><input class="btn" type="submit" value="submit" /></td></tr>
			</table>
		</form>

		<div class="hr"><hr /></div>



		<div id="arrays">


			<p>Add a word to an array below:</p>
			<form id="arrayForm" method="post" action="index.php">
			  <input type="text" name="word" maxlength="24" />
			  <input class="btn" type="submit" name="add" value="add" />
			  <input class="btn" type="submit" name="reset" value="clear array" />
			</form>

			<p>
				<?php
					function sanitize($input) {
					  if (!empty($input)) {
						$input = strip_tags($input);
						$input = htmlentities($input);

						return trim($input);
					  }
					  else {
						return FALSE;
					  }
					}

					if (isset($_POST['add'])) {
					  $word = $_POST['word'];
					  $word = strip_tags($word);
					  $word = trim($word);
					  $word = htmlentities($word);
					  
					  if (sanitize($word)) {
						$_SESSION['words'][] = $word;
						for($i=0; $i < count($_SESSION['words']); $i++){
							echo $_SESSION['words'][$i] . " ";
						}
					  }
					  else {
						echo 'Please enter a word';
					  }
					}
					elseif (isset($_POST['reset'])) {
						$_SESSION['words'] = '';
						session_unregister($_SESSION['words']);
					}
				?>
			</p>
		</div>

		<div class="hr"><hr /></div>
	</div>
</body>
</html>

 

 

With this, I haven't got any errors, warnings, notices or whatsoever. BUT the Two forms can't operate saperately on their own.. If I edit First form, the Second disappears, also the other way arround.

 

 

Link to comment
Share on other sites

Use session_unregister()  instead of session_destroy(). As session_destroy() will destroy all your sessions, while session_unregister() will only destroy the mentioned variable from session.

You should not use session_unregister because it's depreciated. Instead you should just use unset to unset the session variable.

 

unset($_SESSION['var']);

Link to comment
Share on other sites

Use session_unregister()  instead of session_destroy(). As session_destroy() will destroy all your sessions, while session_unregister() will only destroy the mentioned variable from session.

You should not use session_unregister because it's depreciated. Instead you should just use unset to unset the session variable.

 

unset($_SESSION['var']);

 

Ok, thanks for the tip.

 

But still my forms won't work separately...

 

It seems like the two 'submit' buttons are working against each other.. Although I used for one the name 'submit', and for the other one the name 'add'. So I don't know were they are encountering each other?! :shrug:

 

Link to comment
Share on other sites

You're using the local value when either form is submitted, so it looks like the data from the other form is wiped out.

 

Remove most of the PHP code from after the forms and put the following at the top of your script (after the header() line):

<?php
function sanitize($input) {
if (!empty($input)) {
	$input = strip_tags($input);
	$input = htmlentities($input);

	return trim($input);
}
else {
	return FALSE;
}
}
$error = '';
if (isset($_POST['add'])) {
$word = sanitize($_POST['word']);

if ($word) {
	$_SESSION['words'][] = $word;
}
else {
	$error = 'Please enter a word';
}
}
if (isset($_POST['reset'])) {
$_SESSION['words'] = '';
unset($_SESSION['words']);
}
if(isset($_POST['name'])){
$name = sanitize($_POST['name']);

if (sanitize($name)) {
	$_SESSION['name'] = $name;
}
}

?>

 

Change the login form to be:

<form id="login" action="" method="post">
    <table id="table">
        <tr><td><h2 class="HelloTitle">Hello <?php echo $_SESSION['name']; ?></h2></td></tr>
        <tr><td> </td></tr>
        <tr><td><label for="name">Name:</label><br /><br />
        <input id="name" name="name" type="text" maxlength="34" /></td></tr>
        <tr><td><input class="btn" type="submit" value="submit" /></td></tr>
    </table>
</form>

 

And the next form to be

<form id="arrayForm" method="post" action="">
    <input type="text" name="word" maxlength="24" />
    <input class="btn" type="submit" name="add" value="add" />
    <input class="btn" type="submit" name="reset" value="clear array" />
</form>

<p>
<?php
    if ($error != '') {
        echo $error;
    }
    if (isset($_SESSION['words'])) {
        echo implode(' ',$_SESSION['words']);
    }
?>
</p>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.