I'm able to pass a variable from a link on my first page to the 2nd page, but i'm having a hard time passing that variable into the third page. How can i pass the variable to the third page?
First Page is a HTML Page
Go to Page Two
Second Page is a PHP Page
<?php
echo $_GET["Page"];
?>
Third Page is a PHP Page
???
To pass a variable along from one page to another you can choose from multiple options:
Sessions
Cookies
Storage (File or Database)
Sessions
The first page:
<?php
// Prepare to use sessions (This should be at the top of your page)
session_start();
if (!isset($_SESSION['test']) && isset($_GET['test'])) {
$_SESSION['test'] = $_GET['test'];
header("Location: secondpage.php");
exit;
}
The second page:
<?php
session_start();
// Output session
var_dump($_SESSION['test']);
Cookies
First page:
<?php
if (isset($_GET['test'])) {
setcookie('test', $_GET['test'], time() + (86400 * 30), "/");
}
Second page:
";
echo "Value is: " . $_COOKIE['test'];
}
Files and Databases
This is too big a subject to discuss in one answer. However, there are tons of tutiroals that can learn you all you need to know about PHP and MySQL.
Example
First page:
Go
Second page:
<?php
session_start();
if (isset($_GET['page'])) {
$_SESSION['page'] = $_GET['page'];
}
header("Location: page_three.php");
exit;
?>
Third page:
<?php
session_start();
// Will echo two
echo $_SESSION['page'];
?>
Resources
PHP - Sessions
PHP - Cookies
You have two main options:
you can keep linking the pages with url parameters
Go to Page Two
Go to Page Three
This solutions is quite inconvenient if you want to keep the "page" value for a long term
The best solution as user1234 said is to store the value in the session.
if your first page MUST be html and you can't store it at begining you can do in any of your php pages
$_SESSION['Page'] = $_GET["Page"];
Related
For example,
STEP 1:
I am browsing a page without logging in and my last page before logging in is beforeLogin.php
STEP 2:
Now my prob is when i logged in i am redirecting to the index.php page. Instead i should be redirected to the last page what i had browsed.
That is from the above example is should redirected to beforeLogin.php
How should this can be done.
Any help will be appreciable.
thanks in advance
You would need a way to keep track of the web pages visited, so you can back-reference to the last page the user has browsed to.
When I think of tracking a user's session across multiple-pages I, like every other PHP programmer, think of using sessions.
A possible way could be to store the link visited into a session variable and then when the user reaches the login.php page (the page to login into) provide a header redirect to $url given by the session variable.
NOTE: Below code snippets, have not been tested or compiled.
You can paste this code into all your pages on your website:
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVER variables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com/$url"); // perform correct redirect.
The simplest solution by far is simply to have:
<input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
Then redirect to that address once they log in.
However, this is only good if you have a login box on every page. I personally do and I find it works quite well.
you can make login form action lead to the current page.
that's plain and simple
Say, your page looks like
<?
include "auth.php"; // you need it anyway, as you want to control user's auth
//the rest of the page;
?>
<form method="POST">
login form
</form>
<?
//the rest of the page;
if (!empty($_SESSION['user_id'])) echo "Welcome registered user!";
?>
and auth.php would take care of login check and then redirect to a current page is not a big deal
<?
if (isset($_REQUEST[session_name()])) session_start();
if (isset($_POST['auth_name'])) {
//password checking
if (pass ok) {
session_start();
$_SESSION['user_id'] = $row['id'];
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
something like this
no extra session required.
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVER variables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com$url"); // perform correct redirect.
I have two pages which I need to pass session information between them. Here's page one.
<?php
session_start();
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
Page two.
<?php
session_start();
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Page two is in a different directory (same domain), and has Windows Authentication on. Page one is using anonymous authentication. From the output I can see the session ID is the same, but page one doesn't echo the test variable set from page two.
I'm running PHP in IIS 8.5 on Server 2012 R2.
Any help is appreciated.
To clarify, I am calling page two first, and page one will not show the variable.
You need to initialize the variable with a value first before using it.
First run page2.php so, that value of $_SESSION['test'] is set to test
using $_SESSION['test'] = 'test';
Now, run page1.php there you can see the value of $_SESSION['test']
using echo $_SESSION['test']."<br>";
you can also follow these steps to make use of session in a simple way so that you don't have to do session_start() again and again
1. config.php having session_start();
2. page1 having include_once('config.php');
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
3. page2.php having include_once('config.php');
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Try closing your session in the first page, it's possible that your session file is still open when the second page is called, so you're not seeing the data. Try adding session_write_close() to the first page called.
http://php.net/manual/en/function.session-write-close.php
The issue was with permissions on the session save path. I changed the path to where both directories could write and it works.
I have multiple pages. On each page I have a list of items. So on Page 1, I have Items 1-10. On page 2, I have items 11-20. It is NOT ten items per page but the point is that page 3 should start off with the number immediately following the last number on page 2.
The pages have a simple numeric variable so the counting of items works fine per page but I want it to be set in one place.
I am brand new to php so my question is mostly about how and where to store a variable that would keep track of this item count over multiple files?
Desired behavior:
Let's say Page 1 has 5 items and my counter variable on page 1 sets them to be correctly numbered to:
1.apple
2.grape
3.pear
4.banana
5.orange
But let's say I need to remove banana. When I do so, the current counter on page 1 works fine and sets to:
1.apple
2.grape
3.pear
4.orange
But on page 2 the counter variable is set to 6 and has:
6.mango
7.peach
8.watermelon
I want to set the persistent variable so that when I remove a fruit, it will update not only the current page the removed fruit is on but every page after that. So it would adjust page 2 to:
5.mango
6.peach
7.watermelon
Would sessions make that change permanent or would it disappear with the user's cookie?
<?php
//On the first line of your PHP code
session_start();
//then store your variable in the SESSION array.
$_SESSION['yourCountersNameHere'] = 56;
//You should now have acces to its value from page to page like this
echo $_SESSION['yourCountersNameHere'];
//Should display 56.
?>
you could also store it in a DB. and then check in the DB on every page.
//to connecte
$m_DB = new PDO("mysql:host=".$Host.";dbname=".$DBName."; charset=utf8", $Login, $Password);
//check value in DB
$query = "SELECT * FROM table";
$stmt = $m_DB->prepare($query);
//$param here is empty but normaly it would be a array for all your params like this:
//$param = array('rowname'=>$value, 'rowName2'=>$value2, .........and so on for all the $params you use.);
$getCounter = $stmt->execute($param);
while($Counter = $getCounter->fetch())
{
//Display the value in your MySQLDB.
echo $Counter['table.rowName'];
}
If you use another DB type than MYSQL the code should be fairly the same exepte for a couple of changes you'll have to make.
You can look at http://www.php.net/manual/en/session.examples.basic.php for more information using sessions.
Any variable that is placed in a session will be persistent for the user as long as their cookie doesn't expire.
On each page of your site that will access/change your page variable, add this code at the very beginning:
<?php
//On the very first line of each file using the variable
session_start();
Then for accessing/setting see the following:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
This code will spit out one from the session variable 'views'
To check if something has been set:
<?php
session_start();
if(isset($_SESSION['views']))
//do something
Clearing out the session variables:
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>
Deleting the session:
<?php
session_destroy();
?>
Best site in the universe : http://www.w3schools.com/php/php_sessions.asp
I'm trying to update session variables via some anchor tags to keep track of information to display. I currently am using $_GET variables, but would like to keep a clean address bar as this is a single page app. What I can't figure out is how to update the session variable and have it refresh the page and update the variable.
This is the only way I can think to do it, but it's not working as I expected.
Use a click.php file that grabs the $_GET variable and applies it to a session variable and redirects back to the index.php file.
index.php
<a href="click.php?msv=PhotoCount" >Photo Count</a>
click.php
if ( !empty($_GET['msv']) ) {
$msv = $_GET['msv'];
$_SESSION['meta'] = $msv;
header('Location: /');
}
Any thoughts?
You should start the php session explicitly at the beginning of both index.php and click.php, like this .. only then your session data will persist
<?php session_start(); ?>
If you choose to do it, consider removing session data later using this snippet
<?php session_destroy(); ?>
Add this to the top of index.php:
<?php
if(isset($_GET['msv']) && !empty($_GET['msv'])){
session_start();
$_SESSION['msv'] = $_GET['msv'];
}
?>
and give the target location of the link to the same page. So, there is no need of the second page.
If you are to use both the pages, give the target location of the link to the second page and add the this line at the end of the if statement.
header('Location: /index.php');
For example,
STEP 1:
I am browsing a page without logging in and my last page before logging in is beforeLogin.php
STEP 2:
Now my prob is when i logged in i am redirecting to the index.php page. Instead i should be redirected to the last page what i had browsed.
That is from the above example is should redirected to beforeLogin.php
How should this can be done.
Any help will be appreciable.
thanks in advance
You would need a way to keep track of the web pages visited, so you can back-reference to the last page the user has browsed to.
When I think of tracking a user's session across multiple-pages I, like every other PHP programmer, think of using sessions.
A possible way could be to store the link visited into a session variable and then when the user reaches the login.php page (the page to login into) provide a header redirect to $url given by the session variable.
NOTE: Below code snippets, have not been tested or compiled.
You can paste this code into all your pages on your website:
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVER variables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com/$url"); // perform correct redirect.
The simplest solution by far is simply to have:
<input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
Then redirect to that address once they log in.
However, this is only good if you have a login box on every page. I personally do and I find it works quite well.
you can make login form action lead to the current page.
that's plain and simple
Say, your page looks like
<?
include "auth.php"; // you need it anyway, as you want to control user's auth
//the rest of the page;
?>
<form method="POST">
login form
</form>
<?
//the rest of the page;
if (!empty($_SESSION['user_id'])) echo "Welcome registered user!";
?>
and auth.php would take care of login check and then redirect to a current page is not a big deal
<?
if (isset($_REQUEST[session_name()])) session_start();
if (isset($_POST['auth_name'])) {
//password checking
if (pass ok) {
session_start();
$_SESSION['user_id'] = $row['id'];
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
something like this
no extra session required.
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVER variables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com$url"); // perform correct redirect.