How to pass and GET session through URL? - php

I've one variable called numbers which I'm use within a session to identify a user. The link should be like index.php?number=1234567890, where number is the session name to be set and 123456789 is a user id. Can you explain how to do it "Pass through URL and SET as a var"?
index.php?number=1234567890
GET[???]

You can get it from $_GET as follows in your index.php
$number=$_GET['number'];

The variable is accessible in your code as $_GET['number']

To access any GET variables, use $_GET['varname']
Rather than pass a GET variable everytime you redirect, consider using a cookie (setcookie function and $_COOKIE variable) or session variables (start_session function and $_SESSION variable)

Related

Can't access a session variable

I'm using a session variable to keep track of the current site language, three values are possible, 1. EN, 2. RU, 3. ES.
the session variable is set initially in the config file:
$_SESSION['lang'] = 'RU';
but inside my db class I cannot access the variable. My basic understanding is that variables stored in the $_SESSION array are accessible throughout the site.
so what's the problem?
Be sure to call session_start(); before using a session variables.
Before instantiating the class, you need to open the session.
For example:
session_start('NAME_MY_SESSION')

Save Constant GET Variable In A Session

Can some explain to me the best way to store a $_GET variable in a session and the only way the sessions changes is when we verify the data the session is being change to is different from the GET variable.
Currently i have
$tid = clean_get($_GET['tid']);
in a global file which is included on every page the problem with that is the value of $tid will be erased and not stored in a session like i want it to once the user is not on a page with $tid set in the url.
If you get $_GET['tid'] in url then set session again by that new value otherwise restore it from session. Thats it.
session_start();
$tid = (isset($_GET['tid']) && $_GET['tid']!="") ? clean_get($_GET['tid']) : $_SESSION['tid'];
Try this and tell me is it solved?
Use a function like isset() to see if it is being sent. Only then should you replace it:
if(isset($_GET['tid']))
{
$tid = clean_get($_GET['tid'])
// Do stuff to change session data.
}
I think what you are looking for is something like
session_start();
foreach ($_GET as $key=>$value) {
$_SESSION['getValues'][$key] = clean_get($value);
}
This will store all the values in $_GET in the $_SESSION. To retrieve the values later, you just have to use $_SESSION['getValues']['tid'] after calling session_start().
Here I'm assuming that clean_get() is just something that formats and/or escapes data that came in from forms, so calling it on each value before sticking into the session will do all that cleaning when needed.
Note: only call session_start() once, and make sure you do so before doing anything with $_SESSION, otherwise you'll get error messages.

Pass URLs between php pages

I was wondering how I can pass a URL to another web page using PHP. I know if I were to use an HTML form I could have a hidden field with the value being the URL I want to get.
How can I pass the URL along to another page without using a form?
If the page is your own, you could use a session variable.
More details here: http://www.w3schools.com/php/php_sessions.asp
You can use $_SESSION, or a cookie.
PHP has a server global variable which includes a referrer variable.
$_SERVER['HTTP_REFERER']
http://php.net/manual/en/reserved.variables.server.php
You can echo this and it will output the page which the user came from.
You could pass it using a get query,which is in a way form data, so i won't go into it
OR a session variable. On the sender page
session_start();
$_SESSION['url']="http://site.com"
and on the receiver
session_start();
$url=$_SESSION['url'];
unset($_SESSION['url'])l
You can initiate sessions.
Put session_start(); at the top of every page (normally by including a header or function). The session_start(); needs to come before any HTML output.
Then on your referring page, you can save values into the session array:
$_SESSION['URL'] = "myurl.com";
On your second page, you can reference it just like any other variable:
echo $_SESSION['URL'];
Link
somepage.php would then receive the URL in the variable $_GET['url'].

how do I convert a cookie into a variable in php?

Let's say the user logs in, a cookie is sent and the user is redirected to another page.
How do I make it where I can convert the cookie value into a variable in PHP (on the page they are redirected to)?
I want to write the cookie value to a .txt file.
Thanks.
From the docs:
Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C".
Well cookies live in the $_COOKIE global. (When received.)
And writing them to a file would be as boring as:
file_put_contents("var/cookie.txt", $_COOKIE["cookiename"]);
You don't need to convert them just retrieve into a php variable.
<?php
if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
else
echo "You've got some stale cookies!";
echo "Your last visit was - ". $visit;
?>
see here tutorial
Cookies stored in the $_COOKIE array. So if you want to use it, just assign an appropriate cookie key value to your variable, for example:
$name = $_COOKIE['name'];
Cookies can be accessed in the global $_COOKIE array.
Use this:
$newvar = $_COOKIE['cookiename'];
Cookie is available in $_COOKIE as $_COOKIE['key']; get that value and store it in a variable, which can be output to a file.

PHP: Over-writing session variables

Question related to PHP memory-handling from someone not yet very experienced in PHP:
If I set a PHP session variable of a particular name, and then set a session variable of the exact same name elsewhere (during the same session), is the original variable over-written, or does junk accumulate in the session?
In other words, should I be destroying a previous session variable before creating a new one of the same name?
Thank you.
$_SESSION works just like any other array, so if you use the same key each time, the value is overwritten.
Tom,
It depends on how you use the session variable, but it generally means "erasing" that variable (replacing the old value by the new value, to be exact).
A session variable can store a string, a number or even an object.
<?php
# file1.php
session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['favfood'] = array('sushi', 'sashimi');
?>
After this, the $_SESSION['favcolor'] variable and the $_SESSION['favfood'] variable is stored on the server side (as a file by default). If the same user visit another page, the page can get the data out from, or write to the same storage, thus giving the user an illusion that the server "remembers" him/her.
<?php
# file2.php
session_start();
echo $_SESSION['favcolor'], '<br />';
foreach ($_SESSION['favfood'] as $value) {
echo $value, '<br />';
}
?>
Of course, you may modify the $_SESSION variable in the way you want: you may unset() any variable, append the array in the example by $_SESSION['favfood'][] = 'hamburger'; and so on. It will all be stored to the session file (a file by default, but could be a database). But please beware that the $_SESSION variable acts magically only after a call to session_start(). That means in general, if you use sessions, you have to call session_start() at the beginning of every page of your site. Otherwise, $_SESSION is just a normal variable and no magic happens :-).
Please see the PHP reference here for more information.

Categories