php session variable - php

I have written code in PHP where I want to use session variable so that when I will click on submit button then I will get the same session variable after landing to the new page.
But session variable is showing empty after submit.
Below is my code:
session_start();
$i=6;
$_SESSION['testing1']=$_SESSION['testing'];
$_SESSION['testing']=$i;
but value for $_SESSION['testing1'] always shows empty.
1st time it will be empty but after clicking on submit button which is calling same page then it should not give empty value

You are setting $_SESSION['testing'] after $_SESSION['testing1']=$_SESSION['testing'];, so it is still empty while setting. Switch both statements around.

try this
session_start();
$i=6;
$_SESSION['testing']=$i;
$_SESSION['testing1']=$_SESSION['testing'];

Related

Cookie seems to disappear when page changes

I have assigned my variable $_SESSION['Username'] with a value from a form $_POST['Username']. This displays fine on the validation page where the system checks it against the database.
(EDIT: I have checked the validation page, and calling displays the correct value as it should)
I then html redirect using
<meta http-equiv='refresh' content='3; url=".$_GET['Last']."' />
this takes the user back to the page they were on before login,if the details were correct. If not, error message is displayed. This works okay.
However, on the page that the user is redirected to, if i try to
<?php echo $_SESSION['Username']; ?>
No value is displayed, even though it was set on the previous page.
How can I keep the value in SESSION['Username']?
Thanks.
Make sure you're using session_start() to begin a session.

PHP Losing session variables upon going back to previous page

The home page on my website has a form with two fields: Make of the Car (carmake) and Car Model (carmodel). Upon submit, it takes the user to the 2nd page that displays information about the specific make and model that the user has input on the homepage.
I am using post-method to pass the variables to the 2nd page, where I start a new session with the two variables:
<?php
session_start();
$_SESSION['carmake']=$_POST['carmake'];
$_SESSION['carmodel']=$_POST['carmodel'];
?>
This works fine. On this 2nd page, I have a link for a 3rd page that uses the same two session variables. So on the 3rd page, I start the page with the session function and the session variables are available for use. No problem so far.
There are 2 problems I am experiencing from this point onwards:
Problem 1: I have a button on the 3rd page that links back to the 2nd page. When I use this button to go back to the 2rd page, the 2nd page does not echo the session variables. However, if I use the browser 'back' button to go back to the 2nd page, I am asked to "confirm resubmission" which I do and then 2nd page correctly echoes the session variables.
My question 1: How can I design the navigation to previously visited pages without losing the session variables?
Question 2: Is there a way to avoid having to "confirm resubmission" (when using the browser back button) on a previously visited page that uses session variables?
(I am a newbie here and apologize in advance for the long post for what may be a very simple question.)
Question 1 solution:
Change your code
<?php
session_start();
$_SESSION['carmake']=$_POST['carmake'];
$_SESSION['carmodel']=$_POST['carmodel'];
To
<?php
session_start();
if(isset($_POST['carmake']))
$_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel']))
$_SESSION['carmodel']=$_POST['carmodel'];
It will set session value only if form is submit (which is submit from Form1), so when you click back button, it will not set blank values and will keep last session value remains)
Question 2 solution:
<?php
session_start();
if(isset($_POST['carmake']))
$_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel']))
$_SESSION['carmodel']=$_POST['carmodel'];
if(isset($_POST['carmake']) || isset($_POST['carmodel'])){
# Redirect on same page when submitting the form
# will not ask for form submission when click back on browser page
header("Location: page2.php")
exit;
}
Problem 1: Unless you are checking to see if $_POST has values before you assign them, the sessions will get empty values. Turn on a higher error log level and display errors and you should see a notice about an undefined variable. Also, it works when you confirm submission because you are re-POSTing the data to the second page.
Question 1: Check that the POST variables are not empty() before assigning them.
Question 2: Not that I am aware of, you are going back to a page that your browser previously POSTed to and it wants to know if you want to again (to get the "same" results as before)
UPDATE: Also, you should post more code.
I know I am late here, but just thought to share my solution. I hope this helps someone.
Question 1 : Solution
Store the value in session only if form is submitted.
<?php
session_start();
if(isset($_POST['carmake'])) $_SESSION['carmake'] = $_POST['carmake'];
if(isset($_POST['carmodel'])) $_SESSION['carmodel'] = $_POST['carmodel'];
/* Rest of the code goes here */
Question 2 : Solution (easy alternative)
Cache control and Session cache limiter prevents form resubmission when using browser back button.
<?php
session_start();
header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");
/* Rest of the code goes here */
~ Full code ~
<?php
session_start();
header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");
if(isset($_POST['carmake'])) $_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel'])) $_SESSION['carmodel']=$_POST['carmodel'];
/* Rest of the code goes here */

Pass url variable on to next page?

I need some help on passing a url php variable onto the next page. I've tried searching throughout the site for help and I've spent a lot of time trying to figure this out with no luck. Basically I need to be able to change the paypal link button id on page 2 with the url variable from page 1.
The variable is initially passed along with the URL: http://www.example.com?p=paypalbuttonid
I would like to store and pass that "p" variable on to the next page. I don't want to pass the variable onto page 2 with a link. I would prefer to store the variable and recall it on page 2.
Page 1 code (above html):
<?php
session_start();
$_SESSION['paypal'] = $_GET['p'];
?>
Page 2 code (above html):
<?php
session_start();
$p = $_SESSION['paypal'];
?>
I'm calling the variable in a link on page 2 (body):
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=<?php echo $p ;?>" target="_blank" class="btn">
I'm not sure what I'm dong wrong but I'm a complete newbie to PHP so please help! The variable shows up blank in the URL on page 2. Thank you! - Chad
First, you should make sure you dont have any output before session_start(), it is safe to put session_start () at the top of the page , especially if you use php code in .html, since you may have output without awareness, and session wont work if you have anything output to the browser before session_start()
according to php.net:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
and you should check if you cookie is enabled.
Secondly, var_dump ($_SESSION); to see if you can get anything

Stop refresh page

I have this kind of href:
DOWN</td>
When I click on the links I lost datas entered before in a form,what is the way to prevent this?
Thanks!!!
You can either set a cookie:
setcookie('savedData' , $whateverYouWantToHaveSaved, time()+60*60*24*7);
or use a session
session_id();
session_start();
$_SESSION['savedData'] = $whateverYouWantToHaveSaved;
Ref:
cookies: http://www.php.net/setcookie
or
sessions: http://www.php.net/manual/en/ref.session.php
From there you will recall your data via either:
$savedData = $_COOKIE['savedData'];
or
$savedData = $_SESSION['savedData'];
The rest you will need to learn on your own.
You could use a hidden textbox inside the form and when you click on the link you call javascript function to set the value of the hidden input-field and that function submits the actual form. After that you could access the hidden input value from PHP.
DOWN</td>

How to stop php empty field message to appear when page open

i am using this code to print empty filed error
if(isset($_POST['submit'])){
$oth1inp= new CheckInputFieldsAll();
$oth1inp->other1=$_POST['other'];
echo $oth1inp->chkInputOtherOne();
}
this code is on page B, to print error when i submit page B.
But when i go from page A to B it prints the error.
as the page opens error message shows while it should show when i submit page B.
This question also sets some context:
how to stop my php page from continuing when field is empty
If I understand the problem correctly you need some way of identifying whether it's pageA or pageB POSTing data to pageB.
You could include a hidden form input in the forms of both pages and check for it in the $_POST array as you're checking for the submit variable at the moment.
Another way of doing it would be to change the name of the submit button so that in the form on pageA it's called "submitA" and from pageB it's called "submitB"

Categories