Storing Session information and linking to new page on click - php

I hope this isn't stupidly simple. I am completely new to web dev.
I have list items that I styled as buttons.
I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable.
How do I accomplish this/ is there a better way to accomplish the same thing?

Sessions Step By Step
1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this
<?php
session_start();
?>
2 - Set your session inside a page and then you have access in that page. For example this is page 1.php
<?php
//This is page 1 and then we will use session that defined from this page:
session_start();
$_SESSION['danish']='danish';
?>
3- Using and Getting session in 2.php
<?php
//In this page I am going to use session:
session_start();
if($_SESSION['name']){
echo 'Your name variable Is Here! :) ';
}
?>
In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this
First Page
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Second page
<?php
session_start();
echo $_SESSION['myvar'];
?>

Related

Carrying Variables Between 3 PHP Pages

I am creating a set of 3 pages, each with a dropdown menu. The values from the previous dropdown menus will populate the next dropdown menu. I understand that I must use session variables to hold the value of each dropdown menu into later pages. As a simple test, I echo the variables to see if they have been carried over -- and that's where the problem is.
I have three different files: choose_cc.php, choose_uni.php, and choose_major.php
The value from the dropdowm menu in choose_cc.php does get echoed in choose_uni.php -- however the value from choose_cc.php does NOT get carried over into choose_major.php -- despite me storing it into a session variable.
the flow of pages is like this;
choose_cc.php --> choose_uni.php --> choose_major.php
Each php file has it's own form. The problem lies in when I try to call the value from choose_cc.php into choose_major.php, i have issues.
The name of the form on choose_cc.php is choose_cc.
The name of the form on choose_uni.php is choose_uni.
So for example, in choose_uni.php, I retrieve the value from the dropdown menu on the previous page (choose_cc.php) like this:
$_SESSION['choose_cc'] = trim($_GET['choose_cc']); //fetches cc from previous page
$user_cc = $_SESSION['choose_cc'];
echo $user_cc;
and when I echo it as I did above, it works! Okay perfect!
But when I head onto choose_major.php, I try retrieving the value again from the form, but to no avail like this;
echo $_SESSION['choose_uni']; //this works
echo $_SESSION['choose_cc']; //this doesn't work
I have made sure to store to do session_start() on the beginning of each page as well.
Please help me out! this is driving me insane!
Create a new "see.php" with this:
session_start();
print_r($_SESSION);
and execute it after each choose_cc.php, choose_uni.php and choose_major.php to take a look the session you have after you run your programs.
Simple as this:
a) add session_start(); at the beginning of ALL the involved pages. Some weird stuff happens sometimes if you put a space before it, or even a new line. So be sure it is the very first thing in your script.
<?php
session_start();
?>
b) if needed, check the variable to save into session for not empty(); That way you can be sure the session variable contains something, unless of course you want explicitly to be empty.
c) then load the session variable. You can temporary check it with a var_dump();
<?php
session_start();
if (!empty(trim($_GET['choose_cc'])))
{
$_SESSION['choose_cc'] = $_GET['choose_cc'];
}
var_dump($_SESSION['choose_cc']);
?>

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

How to transfer php session variables from a page to an iframe inside the page

So I have a webpage with session variables, for example one variable is:
$_SESSION['name'] = 'testname';
I have an iframe within the page, and want to use this session variable in it. For a start, I am trying to just do this:
echo $_SESSION['name'];
I have not found a way to transmit the session variable into the iframe page.
INFORMATION (IMPORTANT!):
I have used session_start(); on both the main page and the iframe page, and I have also tried using session_write_close(); on both pages. The pages are in the same domain. Please answer. Thank you in advance!
What ever page is referenced in the iframe, if it exists on your the same domain will have access to the same session information on the hosting site so long as that page has session_start() called on it.
Example.
page1.php
<?php
session_start();
$_SESSION["HELLO"] = "WORLD";
?>
<html>
<iframe src='page2.php'/>
</html>
page2.php
<?php
session_start();
echo "HELLO ".$_SESSION["HELLO"]; // will output HELLO WORLD
This is only true for sites that have access to the same cookies and the same session store.

Using php session variable in different pages

I created a session variable in a php page 'session1.php' as follows
<?php
session_start();
$_SESSION['name']="piklu";
?>
now I want to access the session value in another page say 'session2.php'.i need suggestion for that.
what I have done in 'session2.php' is as follows.
<?php
include('session1.php');
echo $_SESSION['name'];
?>
The above code is working but only when session1.php and session2.php doesn't contain any html tags.if it is then all html tags from 'session1.php' will be copied to 'session2.php'. Is there any other way to do that?
You don't need to include all of session1.php. instead, just call session_start() at the start of session2.php.

Categories