Learn PHP and MySQL with AJAX in a weekend by Blerton Abazi

Learn PHP and MySQL with AJAX in a weekend by Blerton Abazi

Author:Blerton Abazi [Abazi, Blerton]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2017-04-18T04:00:00+00:00


Accessing Cookies with PHP

PHP provides many ways to access cookies.Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example.

<html>

<head>

<title>Accessing Cookies with PHP</title> </head>

<body>

<?php

echo $_COOKIE[“name”]. "<br />"; /* is equivalent to */

echo $HTTP_COOKIE_VARS[“name”]. "<br />";

echo $_COOKIE[“age”] . "<br />";

/* is equivalent to */

echo $HTTP_COOKIE_VARS[“name”] . "<br />"; ?>

</body>

</html>

You can use isset() function to check if a cookie is set or not. <html>

<head>

<title>Accessing Cookies with PHP</title> </head>

<body>

<?php

if( isset($_COOKIE[“name”]))

echo "Welcome " . $_COOKIE[“name”] . "<br />";

else

echo "Sorry… Not recognized" . "<br />"; ?>

</body>

</html>

example : In the example below, we retrieve the value of the cookie named "user" and display it on a page: <?php

// Print a cookie

echo $_COOKIE[“user”];

// A way to view all cookies print_r($_COOKIE);

?>

In the following example we use the isset() function to find out if a cookie has been set:

<html> <body>

<?php

if (isset($_COOKIE[“user”]))

echo "Welcome " . $_COOKIE[“user”] . "!<br>"; else

echo "Welcome guest!<br>";

?>

</body> </html>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.