Reputation: 433
I am unable to access value of my Php Sessions, I have looked at other post on Stack tried using there solution, but cannot solve it, I first check if cookie is allowed, if cookie is allowed I get the cookie, but if it is not I create the session and access the session.
BUT I CANNOT ACCESS THE SESSION THAT WAS CREATED
my code..
<?php
session_start();
if ($_COOKIE > 0) //check if cookie is enabled {
//have created the cookie works well
} else { //then if cookie not found, use session, I turned off my browser cookie accept to test and code session
if(isset($_SESSION['shar])
&&
!empty($_SESSION[shar]) &
isset($_SESSION[language])
&&
!empty
($_SESSION['language'])
{ //create session if not found
$_SESSION['Shar'] = 'news';
$_SESSION['language] = 'English';
} else { //view session content
var_dump($_SESSION);
//I GET AN EMPTY ARRAY
}
}
Upvotes: 0
Views: 686
Reputation:
What is a Cookie ?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
Note: The
setcookie()
function must appear BEFORE the tag.
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
The above example created a cookie named "user" with the value "John Doe". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).
Check if Cookies are Enabled
The following example creates a small script that checks whether cookies are enabled. First, try to create a test cookie with the setcookie()
function, then count the $_COOKIE
array variable:
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
What is a PHP Session ?
Session variables hold information about one single user, and are available to all pages in one application.
A session is started with the session_start()
function.
Session variables are set with the PHP global variable: $_SESSION
.
session_start();
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
Note: The
session_start()
function must be the very first thing in your document. Before any HTML tags.
Get PHP Session Variable Values
Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page (session_start()
).
Also notice that all session variable values are stored in the global $_SESSION
variable:
print_r($_SESSION);
Importand part
You are checking for cookies but trying to get session values! The code in question is a Collected code from there and there, I recommend you to read about php sessions and cookies and see some tutorials to write your own code.
See : https://www.php.net/manual/en/reserved.variables.session.php See : https://www.php.net/manual/en/reserved.variables.cookies.php
Upvotes: 2