Unit - 3 PHP - 3
Unit - 3 PHP - 3
date(format,timestamp)
Parameter Description
format Required. Specifies the format of the timestamp
Timestamp Optional. Specifies a timestamp. Default is the
current date and time
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
</body>
</html>
OUTPUT
Today is 2021/05/24
Today is 2021.05.24
Today is 2021-05-24
Today is Monday
PHP Time
• Here are some characters that are commonly used
for times:
– H - 24-hour format of an hour (00 to 23)
– h - 12-hour format of an hour with leading zeros (01 to
12)
– i - Minutes with leading zeros (00 to 59)
– s - Seconds with leading zeros (00 to 59)
– a - Lowercase Ante meridiem and Post meridiem (am
or pm)
<?php
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
OUTPUT
The time is 02:44:18pm
Date With mktime()
• The PHP mktime() function returns the Unix
timestamp for a date.
• The Unix timestamp contains the number of
seconds between the Unix Epoch (January 1 1970
00:00:00 GMT) and the time specified.
<?php
$d=mktime(11, 14, 54, 3, 11, 2005);
echo $d."<br>";
echo "Created date is " . date("Y-m-d h:i:sa", $d);
OUTPUT
1110536094
Created date is 2005-03-11 11:14:54am
Created date is 2005-03-11 11:14:54am
Date From a String With strtotime()
• The PHP strtotime() function is used to convert a
human readable date string into a Unix timestamp
(the number of seconds since January 1 1970
00:00:00 GMT).
strtotime(time, now)
<!DOCTYPE html>
<html>
<body>
<?php
$d=strtotime("10:30pm May 24 2021");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
OUTPUT
Created date is 2021-05-24 10:30:00pm
<!DOCTYPE html>
<html>
<body>
<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>
OUTPUT
</body> 2021-05-25 12:00:00am
</html> 2021-05-29 12:00:00am
2021-08-24 02:54:16pm
PHP Cookies
PHP Cookies
• A cookie is often used to identify a user.
• A cookie is a small file that the server embeds on
the user's computer (clinet browser).
• 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.
• A cookie is created with the setcookie() function.
<?php
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];
}
?>
<p><strong>Note:</strong> You might have to reload the page to see the value of the
cookie.</p>
</body>
</html>
Update a Cookie
<?php
$cookie_name = "user";
$cookie_value = “newuser";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
Delete a Cookie
• To delete a cookie, use the setcookie() function
with an expiration date in the past:
<!DOCTYPE html>
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Show all Cookies
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_COOKIE);
echo "<br>";
foreach($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
?>
</body>
</html>
PHP Sessions
• A session is a way to store information (in variables) to be used
across multiple pages.
• Unlike a cookie, the information is not stored on the users
computer.
• PHP Session : When you work with an application, you open it, do
some changes, and then you close it. This is much like a Session.
• The computer knows who you are. It knows when you start the
application and when you end.
• But on the internet there is one problem: the web server does not
know who you are or what you do, because the HTTP address
doesn't maintain state.
• Session variables solve this problem by storing user information to
be used across multiple pages.
• By default, session variables last until the user closes the browser.
• So; Session variables hold information about one single user, and
are available to all pages in one application.
PHP Sessions
• An alternative way to make data accessible across the various pages
of an entire website is to use a PHP Session.
• A session creates a file in a temporary directory on the server
where registered session variables and their values are stored. This
data will be available to all pages on the site during that visit.
• The location of the temporary file is determined by a setting in
the php.ini file called session.save_path. Before using any session
variable make sure you have setup this path.
• When a session is started following things happen −
– PHP first creates a unique identifier for that particular session which is a random
string of 32 hexadecimal numbers such as 3c7foj34c3jj973hjkop2fc937e3443.
– A cookie called PHPSESSID is automatically sent to the user's computer to store
unique session identification string.
– A file is automatically created on the server in the designated temporary directory
and bears the name of the unique identifier prefixed by sess_ ie
sess_3c7foj34c3jj973hjkop2fc937e3443.
Start a PHP Session
• A session is started with
the session_start() function.
• Session variables are set with the PHP global
variable: $_SESSION.
• Session id will get with session_id() function.
<?php
session_start();
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>
show all the session variable values
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>
Destroy a PHP Session
To remove all global session variables and destroy the session,
use session_unset() and session_destroy().
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
echo "All session variables are now removed, and the session is destroyed."
?>
</body>
</html>
HTTP Headers
• The header() function is an inbuilt function in PHP which
is used to send a raw HTTP header.
• The HTTP functions are those functions which manipulate
information sent to the client or browser by the Web
server, before any other output has been sent.
• The PHP header() function send a HTTP header to a client
or browser in raw form.
• Before HTML, XML, JSON or other output has been sent
to a browser or client, a raw data is sent with request
(especially HTTP Request) made by the server as header
information.
• HTTP header provide required information about the
object sent in the message body more precisely about the
request and response.
HTTP Headers
void header ( string $headerstring [, bool $replace = true
[, int $http_response_code ]] )
• headersring: The header string.
• replace: The optional replace parameter indicates
whether the header should replace a previous similar
header, or add a second header of the same type. By
default it will replace, but if you pass in FALSE as the
second argument you can force multiple headers of the
same type.
• http response code: Forces the HTTP response code to
the specified value. Note that this parameter only has an
effect if the string is not empty.
Header string
<?php
header('WWW-Authenticate: Negotiate');
header('WWW-Authenticate: NTLM', false);
?>
EX-1: Redirecting to a Different Location
<?php
// PHP program to describes header function
?>
headers_list() Function
• The headers_list() function returns a list of response
headers to be sent to the browser.
headers_list()
Ex-2: To prevent caching
<?php
// Set a past date
header("Expires: Sun, 25 Jul 1997 06:02:34 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<html>
<body>
<p>Hello World!</p>
header_remove("Pragma");
?>
headers_sent() Function
• The headers_sent() function checks if/where headers
have been sent.
headers_sent(file,line)
Parameter Description
File Optional. If the file and line parameters are set, headers_sent() will put the
PHP source file name and line number where output started in the file and
line variables
Line Optional. Specifies the line number where the output started
<?php
if (!headers_sent()) {
header("Location: https://www.w3schools.com/");
exit;
}
?>
Sending Content Types Other Than HTML