How to pass a Variable through a session - php

I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.

Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>

remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u

Related

Post data that can be accessed by multiple pages

I am trying to find a way for multiple pages to access the data provided from a single HTML form submission.
Currently, I have 4 pages...
index.php (containing my HTML form)
functions.php (where the HTML
form sends data to)
results1.php
results2.php
Can anyone please let me know where I'm going wrong or point me in the right direction?
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit">
</body>
</html>
functions.php
<?php
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php include_once("functions.php") ?>
</head>
<body>
<?php echo $value1, $value2; ?>
</body>
</html>
You can do it using PHP Session.
Try with the given code below.
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
Value 1: <input type="text" name="value1"><br>
Value 2 :<input type="text" name="value2"><br>
<input type="submit">
</body>
</html>
functions.php
<?php
session_start();
$_SESSION["value1"] = $_POST["value1"];
$_SESSION["value2"] = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php session_start(); ?>
</head>
<body>
<?php echo 'Value 1:'. $_SESSION["value1"]; ?><br>
<?php echo 'Value 2:'. $_SESSION["value2"]; ?>
</body>
</html>

i keep coming back to login page in php

newbie here...
so yeah, i already tried searching all those page-related about my question, but im still stuck...
anyway, my problem is that i always keep getting back at my login page, which is my index.php
oh btw, im still using PHP version 4.4.8
here is my code for my problematic main page, main.php
<?php
session_start();
include '../config.php';
if(!isset($_SESSION['admin'])){
header("location:index.php");
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>KSP Setia Finance</title>
</head>
<body>
<h1>test page</h1>
</body>
</html>
and here is my login page code, which is index.php
<?php
session_start();
include '../config.php'; ?>
<!DOCTYPE html>
<html >
<head>
<title>Login Form</title>
</head>
<body>
<div class="login">
<h1>Login</h1>
<form action="login_act.php" method="post">
<input type="text" name="username" placeholder="Username" required="required" />
<input type="password" name="password" placeholder="Password" required="required" />
<button type="submit" name="login" value="Login" class="btn btn-primary btn-block btn-large">Log In</button>
</form>
</div>
<script src="js/index.js"></script>
</body>
</html>
since everyone asking, here my login_act.php, already inserted with session_start
<?php
session_start();
include('../config.php');
if(isset($_POST['login'])){
$user = mysql_real_escape_string(htmlentities($_POST['username']));
$pass = mysql_real_escape_string(htmlentities(md5($_POST['password'])));
$sql = mysql_query("SELECT * FROM user WHERE username='$user' AND password='$pass'") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
echo 'User not found';
}else{
$row = mysql_fetch_assoc($sql);
if($row['level'] == 1){
$_SESSION['admin']=$user;
echo '<script language="javascript">alert("u are Login as Admin!"); document.location="index.php";</script>';
}else
echo 'sorry, u cant access this one';
}
}
?>
print value of $_SESSION on main.php and check if there is any key as 'username' and check login.php, what values are you storing in $_SESSION array
so i recently asking my friends, and here is the results:
all i need is just put those $SESSION_START above all, or make another php and link them all. so here my latest result that worked :
main.php
<?php
include 'access.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<title>KSP Setia Finance</title>
</head>
<body>
<h1>test page</h1>
</body>
</html>
access.php
<?php
session_start();
if(!isset($_SESSION['admin'])){
echo '<script language="javascript">alert("you must Login!"); document.location="../index.php";</script>';
}
?>
and last, config.php
<?php
session_start();
mysql_connect("localhost","root","");
mysql_select_db("koperasi");
?>
i deleted that broken login_act.php, and making all the page i had to be linked directly with the access.php, which make it easier to manage the session. thank you to all that bear with my php problem and stupidity. hope this all gonna help those who still wandering and asking the same question.

Values input in html form not taken by php

I tried to execute the following html code:
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="listing.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
And this is the code of the associated php file (listing.php):
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php echo $_POST["user"]; ?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I was able to get the form and enter values as shown below:
Form Input
But, when I clicked 'Send Message', it displays only:
Welcome
Your email address is:
Without the values that I entered through the form.
When I tried to run the php file directly from the local host (http://localhost/listing.php), I received these error messages:
Welcome
Notice: Undefined index: user in C:\xampp\htdocs\listing.php on line 7
Your email address is:
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
I even modified the php code as follows, but still got the same output:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit'];)) {
session_start();
$user = $_POST['user'];
echo "$text";}else {echo 'Could not load text!';}?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I would really appreciate it if you could give some advice to make it work.
Thanks
if(isset($_POST['submit'];)) should be changed,so you check if address and user is isset. Furthermore you normally don't want ; in your if statements.
Here i have a optimized version for you. the !empty is added so we also check if the inputs are not empty.
if (isset($_POST["name"] && isset($_POST["address"])) {
if (!empty($_POST["name"] && !empty($_POST["address"]) {
// execute code as u would
}
}
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
This is caused by the use of the ';' in the if condition.
Remove that and you should be good. for now.
Try this code:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit']))
{
session_start();
$user = $_POST['user'];
echo $user;
echo "<br><br>Your Email Address is: ".$_POST['address'];
}
else
{
echo 'Could not load text!';
}
?>
<br>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send" name="submit"></p>
</form>
</body>
</html>
In your second code block $text isn't defined anywhere. Do you mean to have $user?
<html>
<head>
<title>Listing Reading input from a form</title>
</head>
<body>
Welcome
<?php
if(isset($_POST['submit'])) {
session_start();
echo isset($_POST['user']) ? $_POST['user'] : "User";
} else {
echo "User";
}
?>
<br>
Your email address is:
<?php
echo isset($_POST["address"]) ? $_POST["address"] : "Address"
?>
</body>
</html>
Try this and let me know:
<?php
session_start();
if(isset($_POST['submit'])) {
$user = $_POST['user'];
echo "Welcome ".$user;
echo "<br>";
echo "Your email address is: ".$_POST["address"];
}else {
// you may get rid of this block if you like
echo 'Could not load text!';
}
?>
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" name='submit' value="send"></p>
</form>
</body>
</html>

Resetting the session variable in php

I have the following form on "test.php".
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "order.php" looks like this:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.
I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.
Change the new value to SESSION ON your order.php page like below:-
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
Also change test.php code like this:-
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

PHP session variable not working across 2 pages

Creating form and trying to carry variables accross two pages to a results page. I have tried $GET and $POST and it works fine from page1.php to results.php, but when I change to $SESSION the variable isn't passed or echo'd on results.php. Here's the php code for page1 which won't even work directly to results! Not sure if there is a problem with my code or possibly the server?
Page 1.php:
<?php session_start();?>
<?php
$name = $_SESSION['name'];
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>
Please try executing following code snippet
<?php session_start();?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$name = $_POST['name'];
$_SESSION['name']=$name;
}
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Actually in your code snippet value for $_SESSION['name'] was not set .so I have defined value for session variable with posted value from HTML form
You forgot the closing tag in you form, and you don't have a submit button
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
<input type="Submit" value="Submit">
</FORM>
Then in results.php
<?php session_start();?>
<html>
<body>
<?php
$_SESSION['name']=$_POST['name'];
echo $_SESSION['name']; ?>
<br><br>
page 2
</body>
</html>
Then i created this page2.php
<?php session_start();?>
<html>
<body>
Hi, I am still <?php echo $_SESSION['name'];?>
</body>
</html>
Everything is fine in my side.
I think you inverted your variables at this line :
<?php
$name = $_SESSION['name'];
?>
You probably wanted to do :
<?php
$_SESSION['name'] = $name;
?>
first of all you assign the name variable to session variable
<?php session_start();?>
<?php
$_SESSION['name'] = $name;
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?
session_start();
$name = isset($_POST['name'])?$_POST['name']:'';
if($name){
$_SESSION['name']=$name;
}
?>
<html>
<body>
<?php echo $name; ?>
</body>
</html>
Try....
Page 1.php : <?php session_start();
$_SESSION['name'] = isset($_POST['name']) ? $_POST['name'] : '';
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>

Categories