noddycha
noddycha

Reputation: 71

How to access a variable across sessions in PHP?

This may sound a bit catchy, but i want to access variable across sessions. I want to store a variable in a session and want to access it even after i navigate away from my website and come back some time later.

Thanks in advance :) Cheers..

Upvotes: 0

Views: 729

Answers (4)

efe
efe

Reputation: 16

The only way to do this with php is to use cookies because the only data stored in browser's storage are cookies.

setcookie("TestCookie", $value, time()+3600);  /* expires in 1 hour */

After you set the cookie you can get it's value until expiration time with referencing $_COOKIE automatic global variable.

echo $_COOKIE["TestCookie"];

Upvotes: 0

talkol
talkol

Reputation: 12927

PHP has a built in mechanism for using sessions: http://www.php.net/manual/en/book.session.php

here is a super simple example from their tutorial:

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?>

Upvotes: 0

rkmax
rkmax

Reputation: 18143

You can solve this problem with sessions in this tutorial you can get a little explain about that

Upvotes: 0

nwaltham
nwaltham

Reputation: 2084

You need a cookie for that. http://www.w3schools.com/PHP/php_cookies.asp

Upvotes: 1

Related Questions