Reading cookies on localhost - php

i have installed phpmyadmin on mac but there are problem , when i using $_COOKIE[] i see this message in my web page
Notice: Undefined variable: coo in
how can i fix this problem ?
thank you .

I think this could help:
First, you have to set a cookie value:
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);
?>
After that, you can retrieve this value:
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
Source: http://www.w3schools.com/php/php_cookies.asp

user1219391 :
You can check if the cookie value exists using this code:
if (array_key_exists('user', $_COOKIE))
{
$userStatus=$_COOKIE['user'];
}
else
{
$userStatus='Not Set';
}
echo 'The value of Cookie user is '. $userStatus;
With this you can solve your issue...

Related

PHP: Session and Cookies doesn't work (session id change for each page)

I've a problem with Session and Cookies in a PHP project.
I looked for here, but all solutions that I found didn't work.
This is the situation: my code doesn't work only on a specific server.
On that server session id change for each page.
The server use HTTPS and, with phpinfo(), I can see that:
Session Support is enabled
session.auto_start is off
session.cookie_httponly is off
session.cookie_secure is off
I tried this simple code:
A page set.php:
<?php
session_start();
setcookie("testc", "test cookie", time()+3600, '/');
$session_id = session_id();
echo "SESSION id: ".$session_id."<br>";
$_SESSION["test"] = "Test session" ;
if ( !is_writable(session_save_path()) ) {
echo '<br>Session save path "'.session_save_path().'" is NOT writable!';
}
else{
echo '<br>Session save path "'.session_save_path().'" is writable!';
}
?>
A page get.php:
<?php
session_start();
$session_id = session_id();
echo "SESSION id: ".$session_id."<br>";
if( isset($_SESSION["test"]) ){
echo "<br>Test session variable exists:<br>";
echo $_SESSION["test"];
}
else{
echo "<br>Test session variable not exists";
}
if( isset($_COOKIE["testc"]) ){
echo "<br>Cookie:<br>";
echo $_COOKIE["testc"];
}
else{
echo "<br>Cookie not exist";
}
if ( !is_writable(session_save_path()) ) {
echo '<br>Session save path "'.session_save_path().'" is NOT writable!';
}
else{
echo '<br>Session save path "'.session_save_path().'" is writable!';
}
?>
Output for set.php:
SESSION id: q1340a8g2aa3op7v3od3glgkt6
Session save path "/var/lib/php5/sessions" is writable!
Output for get.php:
SESSION id: qu12anislkan3r4vr46rqlopp3
Test session variable not exists
Cookie not exist
Session save path "/var/lib/php5/sessions" is writable!
If I reload one page (get.php or set.php), the session id is the same only for that page.
what could be the problem?
Thanks a lot.
UPDATE:
I discovered that the session id change also in the same page. I saw the same session id because the server gives me a cached version of page. In fact if I add a GET random param to the address, the session id changes.

php SESSION gets destroyed after redirection

I have a page where I set a value to a SESSION but when I redirect to another page
ex. index.php that value I put to that SESSION doesn't exist anymore!
<?php
session_start();
// this is the page where I set a value to a SESSION called var!
$SESSION['var'] = "hello";
if(isset($SESSION['var'])){
echo "Yes it is";
header("location: test.php");
exit();
}
else {
echo "No it isnt";
}
?>
And this is the test.php where I get the SESSION undefined error!
<?php
session_start();
if(isset($SESSION['var'])){
echo "Yes it is";
}
else {
echo "No it isnt";
}
?>
Ass you can see, I put session_start(); in both pages but still nothing!
Any help would be much appriciated,
thank you!
P.S Im using XAMPP
To access session variables you need to access the $_SESSION. Change $SESSION to $_SESSION. Hope this helps.

PHP: Passing variable with cookie

What's the correct way of passing variable using PHP cookie. I can't seem to get it to work? I keep getting "FAILED!"
Here's my code:
On 1st page:
$crpid['ONE']="PAGE1";
$crpid['TWO']="PAGE2";
$crpid['THREE']="PAGE3";
$crp_id = $_SERVER["REDIRECT_URIPART"];
$crp_value = $crpid[$crp_id];
session_start();
setcookie('crpid', $crp_value, time()+3600, "/");
On 2nd page:
if(!isset($_COOKIE['crpid']) && $_COOKIE['crpid']==''){
echo "FAILED!";
}
else{
echo "Cookie ".$_COOKIE['crpid']." is set!";
}

Can I display all the cookies I set in PHP?

I am trying to diagnose an error in my cookies, but the names of the cookies are not what they should be. Is there a way in PHP to print all the cookies that have been set by my domain?
Have you tried:
print_r($_COOKIE)
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
<pre><?php print_r( $_COOKIE ); ?></pre> will do what you want. You might also try phpinfo().
echo $_COOKIE["cookie_name"]; // Print an individual cookie
print_r($_COOKIE); // Another way to debug/test is to view all cookies
You can display all cookies defined by running the following php function:
var_dump($_COOKIE);
if($_COOKIE) {
print_r($_COOKIE); //print all cookie
}
else
{
echo "COOKIE is not set";
}
As with any inputs, security practices should include filtering and validation. Since all cookies are strings, sanitize the strings:
var_dump(filter_input_array(INPUT_COOKIE, FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY))
PHP Docs: https://www.php.net/manual/en/function.filter-input-array.php
if($_COOKIE) {
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
}
else
{
echo "No Cookies are Set";
}
This will check if any cookies are set, if found will iterate through each and print out the cookie name and value

check cookie - if found display link

I want to display a link to a user when he has an cookie named abc in his browser
<?php
$cookie = abc;
if (!$cookie)
{
echo <br><center>click here</center></b>;
}
?>
how could I do this?
thanks
The PHP global $_COOKIE could be used to check the user's cookies
<?php
if (isset($_COOKIE["abc"]))
{
echo '<br><center>click here</center></b>';
}
?>
check http://www.w3schools.com/php/php_cookies.asp
You need to check if it is set in the $_COOKIE variable:
<?php
$cookie = abc;
if(isset($_COOKIE[$cookie]))
{
echo <br><center>click here</center></b>;
}
?>
study the php tag $_COOKIE on php.net
and add the "" into the right places.... like: echo "...

Categories