check cookie - if found display link - php

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 "...

Related

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.

global a $_SESSION variable

I'm trying to make a login session on PHP, but it appears that the $_SESSION['username'] dies inside the IF sentence (I thought $_SESSION where globals by default), and I cant echo it out of the IF
heres My code
if($name=="admin" && $password=="admin")
{
session_start();
$_SESSION['username'],$_SESSION['sesion'];
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
The last echo doesnt print its VALUE, So when I redirect it to another page, the page doesnt take the username value
I'm kind of new in this matter
So dont be so harsh on me :P
How can I do this??
Move session_start() to the top of the file:
// foo.php
<?php
session_start();
//....
if($name=="admin" && $password=="admin")
{
// $_SESSION['username'],$_SESSION['sesion']; // Remove this line
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
When You are using sessions in php You must start it with session_start() in every file/page You want to get or set session values, so just add this line to this file and that file You're redirecting to.
session_start() should be above the if statment. best to put it right below the ?php

Shared variables between php $_GET and $POST

Why page can not get the $temp_kt value? I tested $_SESSION['temp_kt'] and $_ENV['temp_kt'], neither worked.
<?php
$temp_kt=0;
if(isset($_POST['db']))
{
if($_POST['db']=="feedback")
{
global $temp_kt;
$temp_kt=$_POST['temp_kt'];
}
exit();
}
if(isset($_GET['q']))
{
echo "temp_kt=".$temp_kt;
}
?>
You have exit in if(isset($_POST['db'])) which mean that you can't have both if statements. If you want to save that value in session you should use code like this:
<?php
session_start();
if (isset($_POST['db'])) {
if ($_POST['db']=="feedback") {
$_SESSION['temp_kt'] = $_POST['temp_kt'];
}
exit();
}
if (isset($_GET['q'])) {
echo "temp_kt=" . $_SESSION['temp_kt'];
}
?>
session_start function will enable session for you, you need it at the begining when you set and get session values (it will send cookie to the browser - using header - so you can't have any echo before).

Reading cookies on localhost

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...

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

Categories