I'm using $_SESSION for the first time. I think I get the concept, but something isn't working. This is a page that need to load once with POST variables and then, when reloaded (to page through search results), remember the values of the post variables. The two variables would always be set or not set at the same tiem.
//submitted form variable definitions
if (!isset($_SESSION)){
session_set_cookie_params(3600,"/");
session_start();
}
if (isset($POST['word'])) { $name=$_POST['word'];
$_SESSION['word'] = $name; };
if (isset($POST['exact'])) { $exact=$_POST['exact'];
$_SESSION['exact'] = $exact; };
Your POST variables have a mistake. It should have an underscore _ like follows.
isset($POST['word']) // change this
isset($_POST['word']) // to this
isset($POST['exact']) // change this
isset($_POST['exact']) // to this
Otherwise it will always return false.
It is also better to use session_start(); at the top (It is not a problem here).
To further elaborate: $_POST is a superglobal. All except $GLOBALS require an underscore between $ and the method used.
These superglobal variables are:
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
as per the manual:
http://php.net/manual/en/language.variables.superglobals.php
Related
So I have a a variable called json that stores all of the json. I want to load this once in the site, and be able to use it through any php function that I have. So let's say I have this line of code placed at the beginning of index.php:
$json = file_get_contents('http:/xxxxxxxx/bp.json');
I want to be able to use this variable from a function somewhere else in the page, WITHOUT loading it again.
Thanks!
You could store your variables in the PHP session and call it anywhere in your application.
$_SESSION["favcolor"] = "green";
http://www.w3schools.com/php/php_sessions.asp
Also best practices is to destroy and/or clear all your session variables upon exiting your application.
1) Use global variables
<?php
$myVar = 1;
function myFunction()
{
global $myVar;
$myVar = 2;
}
myFunction();
echo $myVar; //will be 2
include 'another.php'; //you can use $myVar in another.php too.
?>
variables PHP manual
2) Use cookies
If you want your variable to be access from any browser window or after loading another page, you have to use COOKIES since HTTP is stateless. These cookies can be accessed via javascript since those are stored in client side browser and can be accessed by client.
<?php
setcookie("myVar","myValue",time()+86400);
/*
86400 is time of cookie expires. (1 day = 86400)
*/
?>
<?php
/*
Getting cookie from anywhere
*/
$myVar = $_COOKIE["myVar"];
echo $myVar;
?>
cookies PHP manual
3) Use a session
Best method to store your variables in server side which is secure than using cookies directly is by using a HTTP_SESSION
<?php
/*
Start a session.
Call this line top of every page you want to use session variables
*/
session_start();
// set variable
$_SESSION["myVar"] = "value";
?>
<?php
session_start();
// Access session variables from another php.
$myVar = $_SESSION["myVar"];
?>
sessions PHP manual
A simple way do this ,you can use Superglobals
Superglobals — Superglobals are built-in variables that are always available in all scopes
I think you must used supergloabls variables before like $_SERVER,$_GET,$_POST etc,and supergloabls variables also include $GLOBALS.
What is $GLOBALS
Note: Variable availability
Unlike all of the other superglobals, $GLOBALS has essentially always been available in PHP.
So you can do this
Get data from json file
Assign data to $GLOBALS
User $GLOBALS variable instead of $json
I am wondering why: without clicking the submit button on a form on my PHP page, the _POST variable is already set.
For example, I have this piece of code on the web page:
if (isset($_POST)){
echo "XXXXXXX";
}
It turns out the XXXXXX is echoed just when the page loads the very first time -- at this point I have of course not submitted any data to the server using POST. Why would this be the case?
As specified on PHP.net, it is automatically created.
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.
To address your code, it is created, but it's empty.
To better test if the user has made a POST request, simply test for an index on $_POST, like isset($_POST['someFieldName'])
Your code should test if it's empty or not:
if(!empty($_POST)){
echo "a";
}
or
if(isset($_POST['someFieldName'])){
echo "a";
}
$_POST is a superglobal array and it is always set. If you do not pass any value to it using post method it will be empty. So set a value to this array and check whether that value is available or not like this:
if(isset($_POST['submit'])){
//Do this...
}
The same is true for the $_GET superglobal. These are always set regardless of HTTP Method. You can check if a request was a POST, GET, PUT, etc by checking the REQUEST_METHOD
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// this was an HTTP POST request
}
Check if the variable is empty or not. $_POST will always be set.
So something like the following should work:
if(!empty($_POST['textFieldName'])){
echo "XXXXXXX";
}
$_POST is superglobal and it's always set as an empty array.
Try this, just to understand better:
if(!is_null($_POST))
{
print_r($_POST);
}
Why is this going to help you to understand? - Because isset checking if a variable is set and is not NULL.
I am trying to fully understand sessions, and I conducted this test:
my code is:
test.php
<?
#session_start();
if(isset($_SESSION['test'])) {
echo "test success";
}
?>
When I enter my cookie manually into my browser using an addon as:
PHPSESSID test
It does not recognise it.
$_SESSION is a "superglobal" (available everywhere) array that is tied with a cookie using a unique session id.
If you're wanting to reference cookie values you've set you'll need to use the $_COOKIE superglobal array.
You can read more about superglobals here: http://php.net/manual/en/language.variables.superglobals.php
And how $_SESSION and $_COOKIE works here:
http://php.net/manual/en/reserved.variables.cookies.php
http://php.net/manual/en/reserved.variables.session.php
You cannot set values in the SESSION by using the browser like that. PHP is the only place you'll be able to set the 'test' key to a value, like true or false.
session_start();
// You could assign this based on the value of a cookie
$_SESSION['test'] = true;
if ($_SESSION['test']) {
// this is a test session
}
Hope that helps.
To see the result of your cookie change, do:
<?
#session_start();
if(session_id() == 'test') {
echo "test success";
}
The cookie contains the session ID, individual session variables are stored on the server, using this ID and the variable name as keys (the default configuration uses a file named after the session ID).
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.
I am developing a php script which contains an html form.
If not all fields are filled in the correct way the script will signal an error and redirect back to the same page with the header function setting an error variable to yes with the get method:
header("Location: registration_page.php?error_empty=yes");
my script has an error handling part in which it highlights the fields containing a mistake, but I would like to keep the value of the fields correctly filled.
I am implementing this feature as I found in this other question:
How can I keep a value in a text input after a submit occurs?
but the problem is that when the page reopens the forms will not contain the old values.
My question is: does anybody know if the header function unsets global variables in the $_REQUEST array?
And do you know what kind of solution could I adopt?Maybe sessions?
Thanks in advance,
Matteo!
$_COOKIES will stay set, but $_POST & $_GET will be destroyed, as the client is moving to a new page. If they need to be retained, they must first be stored into $_SESSION before calling the redirect.
session_start();
$_SESSION['last_post'] = $_POST;
header("Location: http://example.com");
exit();
// On the redirected page, use the stored POST values and unset them in $_SESSION
session_start();
if (empty($_POST) && isset($_SESSION['last_post'])) {
$post = $_SESSION['last_post'];
unset($_SESSION['last_post']);
}
else $post = $_POST;
Does anybody know if the header function unsets global variables in the $_REQUEST array?
No, it doesn't. Cookies ($_COOKIE) will remain.
Obviously $_GET will contain whatever you have in the redirect (eg: $_GET['error_empty'] = 'yes') and$_POST` will be empty because you aren't posting.
So it $_REQUEST will be a combination of $_COOKIE and the new $_GET parameters you set.
You probably shouldn't be using $_REQUEST anyway. Specify exactly where you expect your request parameters to be...
It doesn't, but it does make a new request. A new request means a new $_REQUEST which does not necessarily have all of the old data. $_COOKIE will still be there but $_GET and $_POST will be new meaning that $_REQUEST will reflect that. ($_FILES will also be empty and while it is not in `$_REQUEST, it is another user-supplied value will be reset).
If you want to restore the form, you'll need to either put all of the variables in the url in the header, or use $_SESSION or setcookie and then restore from $_GET, $_SESSION or $_COOKIES.