Solved

In PHP, if a form POSTS to itself, must I use a SESSION variable to get that info to another page?

Posted on 2014-01-07
11
800 Views
Last Modified: 2014-01-07
In PHP, if a form POSTS to itself, must I use a SESSION variable to get that info to another page?
0
Comment
Question by:LB1234
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 75 total points
ID: 39762178
It depends on what you are doing.  I have used session variables, cookies, query string and databases to pass data from page 1 to page 2 when page 1 posts to itself.
Can you tell us what you are trying to accomplish?
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762194
A SESSON variable is definitely the easiest, as long as the other page is within the same domain.  Session variables don't exist across domains.
0
 
LVL 1

Author Comment

by:LB1234
ID: 39762196
Just a simple single-page validation create user form.  To clarify, the POST information in the array is not available to other pages by default without additional steps to transfer the data?
0
 
LVL 1

Author Comment

by:LB1234
ID: 39762198
The form submits to itself.
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762204
Correct.  The POST information is available to the page that is called by the form.

If you want other pages, in that same domain, to be able to access the data, simply put it into a SESSION variable instead of the POST variable.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:LB1234
ID: 39762205
So should i just set the following POST array to a SESSION array to capture the data from the form for an "account created" page which shows what the user entered?

	if(isset($_POST["submit"])) {
	  	$first_name = $_POST["first_name"];
	  	$last_name = $_POST["last_name"];
	  	$user_name = $first_name[0] . "_" . $last_name;
	  	$access_level = $_POST["access_level"];
		$department = $_POST["department"];
		$password = $_POST["password"];
	  	$password2 = $_POST["password2"];
		 	

Open in new window

0
 
LVL 9

Accepted Solution

by:
oheil earned 200 total points
ID: 39762206
I would say: Yes.

Lets say we start with start.php.
We enter the data into the form, press submit, and again we call start.php.
The POST data is than only available inside the start.php during this last call.
From the result page in the browser (after submit, and after start.php again has finished) we open a new page linked inside start.php, say it is named second.php.
We call second.php by clicking on a link produced by start.php.
The script/page second.php does not see any POST data. You need to write the POST data during the second call to start.php into a session. This session remains than accessible in second.php.

Oli
0
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 75 total points
ID: 39762207
That is correct.  If the form submits to itself, then the POST array will NOT be available to other pages without other work such as session variables.
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 225 total points
ID: 39762222
If the first page uses the data, you can get it from the POST array to use, and later set it into a SESSION variable.

$dataval = $_POST["dataval"];
$_SESSION["dataval"] = $dataval;

If all you need to do is pass it on, just

$_SESSION["dataval"] = $_POST["dataval"];

Don't forget to use SESSION_START at the beginning of each script using SESSION variables.
0
 
LVL 1

Author Comment

by:LB1234
ID: 39762239
When I do a var_dump on the page redirected to (account_created.php) it says the array is empty.  Any ideas why this is happening?

<?php session_start();?> 
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/db_connection.php"); ?>


<?php
      
$result = "";

/*$existing_user_check = "SELECT user_name from users";

$result = mysqli_query($connection, $existing_user_check);

	  
while($row = mysqli_fetch_assoc($result)) {
var_dump($result);
}  */
	  
	  
	  $errors = array();  
		
		if(isset($_POST["submit"])) {
	  	$first_name = $_POST["first_name"];
	  	$last_name = $_POST["last_name"];
	  	$user_name = $first_name[0] . $last_name;
	  	$access_level = $_POST["access_level"];
		$department = $_POST["department"];
		$password = $_POST["password"];
	  	$password2 = $_POST["password2"];
		 	
			 if ($password !== $password2) {
				$errors[] = "Passwords do not match, please re-enter";  
					 if (empty($password)) {
						 $errors[] = "Password is empty, please enter a password";
					 }
					 
					  } else {
						$query = "INSERT INTO users (first_name, last_name, user_name,  department, password, password2, access_level) VALUES (";
						$query .= "'{$first_name}', '{$last_name}', '{$user_name}',  '{$department}', '{$password}', '{$password2}', '{$access_level}')";
						$result = mysqli_query($connection, $query);
					
		$first_name = $_SESSION["first_name"];
	  	$last_name = $_SESSION["last_name"];
	  	$user_name = $first_name[0] . $last_name;
	  	$access_level = $_SESSION["access_level"];
		$department = $_SESSION["department"];
		$password = $_SESSION["password"];
	  	$password2 = $_SESSION["password2"];
	
		redirect_to("account_created.php");
					  }
	} else {
  
	  	$first_name = "";
	  	$last_name = "";
	  	$user_name = "";
	  	$access_level = "";
		$department = "";
		$password = "";
	  	$password2 = "";
	  	$message = "Please log in";
	}



?>

<?php
$options = array("user", "administrator");
$html_options = "";
?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="default.css" rel="stylesheet" type="text/css">
</head>

<body>
<div>
<h1>Please create user</h1>


<form action="create_user.php" method="post">
<table  width = "400" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td>Enter First Name</td>
    <td><input name="first_name" type="text" value="<?php echo $first_name ?>"></td>
  </tr>
  <tr>
    <td>Enter Last Name</td>
    <td><input type="text" name="last_name" value="<?php echo $last_name ?>"> </td>
  </tr>
  <tr>
    <td>Enter Password</td>
    <td> <input type="password" name="password"> </td>
  </tr>
  <tr>
    <td>Re-Enter Password</td>
    <td><input type="password" name="password2"></td>
  </tr>
   <tr>
    <td>Set Access level</td>
    <td><select name="access_level" id="access_level">
      
<?php
		
foreach ($options as $option) {
	
	$selected = ($access_level == $option)  ? "selected" : "";
	$html_options .=  "<option value='{$option}' {$selected}>{$option}</option>";
	}

echo $html_options;

?>
    
      
    </select></td>
  </tr>
    <td>Department</td>
  <td><select name="department" id="department">
    <option value="administration">Administration</option>
    <option value="research">Research</option>
    <option value="sales">Sales</option>
    <option value="trading">Trading</option>
  </select></td>
  </tr>
</table>





<input name="submit" type="submit" id="submit">
</form>

</div>




</body>
</html>

Open in new window

0
 
LVL 9

Assisted Solution

by:oheil
oheil earned 200 total points
ID: 39762259
You only read from the SESSION array.
It seems that from line 42
it should be
$_SESSION["first_name"]=$first_name;
...

Oli
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysql sync between 3-4 mysql db 4 25
How do I change the from header when sending a mail 1 18
PHP: concatenate query 13 45
php construct 5 15
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now