Php-mysql Course

Cookies are a way for a server to store information on the user's computer.
A cookie is a small file that the server embeds on the hard drive of the user's computer (it is limited to about 4 KB of total data). Each time the same computer accesses a page with the same browser, it will send the cookie too.

Setting cookies

To set (create) a cookie with PHP, use setcookie() function.
  - Syntax:
setcookie(name, value, expire, path, domain, secure)
"name" - The name of the cookie
"value" - The value of the cookie
"expire" - A Unix timestamp (in number of seconds since 1970) when the cookie expire. You can set this with the "time()" function plus the number of seconds before you want it to expire. If set to 0, or omitted, the cookie will expire when the browser closes.
"path" - The path on the server in which the cookie will be available on. The default value is the current directory that the cookie is being set in. If set to "/", the cookie will be available within the entire web site.
"domain" - The domain that the cookie is available to. For example, to make the cookie available on all subdomains of coursesweb.net (including https://coursesweb.net itself) then you'd set it to '.coursesweb.net'.
"secure" - If set to TRUE, the cookie should only be transmitted over a secure HTTPS connection.
- Only the "name" is required, the rest of parameter are optional.

  Important - cookies must be sent before any output from your PHP script. The setcookie() function must be placed prior to any output, including <html> tags as well as any whitespace.
On success returns TRUE, but if output exists prior to calling this function, setcookie() will fail, return FALSE and warning error (headers already sent ...).

  - Example (sets 3 cookies: "cookie_name1", "cookie_name2" and "cookie_name3"; with a value of "I AM", and different expiration times):
<?php
$value = 'I AM';
$one_hour = 3600;
$one_week = 60*60*24*7;

setcookie("cookie_name1", $value);                               // expire when the browser closes
setcookie("cookie_name2", $value, time()+$one_hour);             // expire in 1 hour
setcookie("cookie_name3", $value, time()+$one_week, "/");        // expire in 1 hour (available within the entire web site)
?>

<html>
...

Retrieve a Cookie Value

To retrieve (access) a value from a cookie, you only need to refer to the $_COOKIE superglobal variable, using the cookie name as the key.
The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received.
To find out if a cookie has been set, use the isset() function.
  - Example:
<?php
if (isset($_COOKIE['cookie_name'])) {
  // get and output the value of "cookie_name"
  $cookie_val = $_COOKIE['cookie_name'];
  echo $cookie_val;
}
else {
  echo 'The cookie: cookie_name is not set';
}

// get and output all cookies for the current website
$cookie_all = print_r($_COOKIE, true);
echo $cookie_all;
?>

print_r($var) displays information about $var variable, but when TRUE is added, print_r($var, true) returns the information rather than print it, and can be transfered to a variable.


• You cannot set and access a cookie in the same page until the setting page has been reloaded or another page has been accessed.

Deleting cookies

To delete a cookie, just set its expiration date in the past.
  - Example :
<?php
setcookie("cookie_name", "", time()-3600);
?>

<html>
...
When deleting a cookie, if you set the "path" and "domain" in the creation cookie, use them again in the deletion cookie.

Cookies are not an indicated way to store information, the user can any time delete them or set its browser to not accept cookies.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
PHP Cookies

Last accessed pages

  1. Simple Admin Login PHP Script (10894)
  2. jQuery parent, children and nth-child() (13826)
  3. Display UL bullets and OL numbers on the right side (8088)
  4. Convert BBCode to HTML and HTML to BBCode with JavaScript (9298)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3445)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide