0% found this document useful (0 votes)
16 views41 pages

PHP Redirection, File Upload, Cookies, and Sessions

The document provides an overview of key PHP functionalities including redirection using the header function, sending emails with mail(), and handling file uploads with $_FILES. It also explains cookies, their creation with setcookie(), and session management using session_start(), session variables, and session destruction. Additionally, it discusses the differences between sessions and cookies, highlighting their respective use cases in web applications.

Uploaded by

afrah mehar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views41 pages

PHP Redirection, File Upload, Cookies, and Sessions

The document provides an overview of key PHP functionalities including redirection using the header function, sending emails with mail(), and handling file uploads with $_FILES. It also explains cookies, their creation with setcookie(), and session management using session_start(), session variables, and session destruction. Additionally, it discusses the differences between sessions and cookies, highlighting their respective use cases in web applications.

Uploaded by

afrah mehar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Free & Open

Source Software
(FOSS)
Module 3
B.voc SD
S6
Redirection in PHP
• The header function in PHP can be used to redirect the
user from one page to another. It is an in-built function
that sends raw HTTP header to the destination (client).
• header( $header_value, $replace_value,
$http_response_code)
• The ‘header_value’ in the function is used to store the
header string.
• The ‘replace_value’ parameter stores the value that
needs to be replaced.
• The ‘response_code’ is used to store the HTTP response
code.
• <?php
• header("Location:
http://www.tutorialspoint.com");
• exit;
• ?>
PHP mail() Function
• mail(to,subject,message,headers,parameters)
PHP File Upload
• PHP allows you to upload single and multiple files
through few lines of code only.
• PHP file upload features allows you to upload
binary and text files both. Moreover, you can have
the full control over the file to be uploaded through
PHP authentication and file operation functions.
• $_FILES -can get file name, file type, file size, temp file name and errors
associated with file.
• $_FILES['filename']['name']
• returns file name.
• $_FILES['filename']['type']
• returns MIME type of the file.
• $_FILES['filename']['size']
• returns size of the file (in bytes).
• $_FILES['filename']['tmp_name']
• returns temporary file name of the file which was stored on the server.
• $_FILES['filename']['error']
• returns error code associated with this file.
<form action="uploader.php" method="post"
enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image"
name="submit"/>
</form>
<?php
$target_path = "e:/";
$target_path = $target_path.basename( $_FILES['fileToUpload']
['name']);

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],
$target_path)) {
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
}
?>
• What is a Cookie?
• A cookie is often used to identify a user. A cookie is
a small file that the server embeds on the user's
computer. 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.
• setcookie(name, value, expire, path, domain,
secure, httponly);
• The setcookie() function must appear BEFORE the
<html> tag
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is set!";
} else {
echo "Cookie '" . $cookie_name . "' is not set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
• name: The name of the cookie.
• value: The value you want to store in the cookie.
• expire-time: It is the number of seconds until the cookie will be kept on the user’s
machine by the browser. After that, it will automatically be deleted. If not set then
the cookie will be preserved by browser until it is open.
• path: It determines for which directories cookie will valid. If you want to access it in
all directories then put it on “/”, i.e. the cookie is accessible in the entire domain.
Otherwise, the cookie will be limited to the subdirectory.
• domain: It is used to define the access hierarchy for the cookie. For example, if you
set this to “yourdomain.com”, it will be accessible via all the subdomains also. but if it
set to “sub.yourdomain.com”, it will be accessible by “sub.yourdomain.com” and its
subdomains.
• secure: It determines how the cookie will be sent, via HTTP or HTTPS. If set to true
then the cookie will be sent via HTTPS only, otherwise, it will be sent via HTTP. Its
default value is false.
• httponly: If it set to true, the cookie is accessible only either via HTTP or HTTPS. That
means the client code (like Javascript) can not access the cookie.
• Deleting Cookie: There is no special dedicated
function provided in PHP to delete a cookie. All we
have to do is to update the expire-time value of the
cookie by setting it to a past time using the
setcookie() function. A very simple way of doing
this is to deduct a few seconds from the current
time.
• setcookie(name, time() - 3600);
<!DOCTYPE html>
<?php

// Set the expiration date to one hour ago


setcookie("gfg", "", time() - 3600);
?>

<html>

<body>

<?php
echo "Cookie 'gfg' is deleted.";
?>

</body>

</html>
PHP Sessions
• A session is a way to store information (in variables)
to be used across multiple pages.
• Session variables store user information to be used
across multiple pages (e.g. username, favorite
color, etc). 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.
• Session variables are hook variables that are global
to the entire logon session. This means you can set
the session variable in any type of hook, and read it
later on
• session_start()
• Session variables are set with the PHP global
variable: $_SESSION.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
Modify a PHP Session
Variable
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
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();
// destroy the session
session_destroy();
?>
</body>
</html>
• The session_id() function is used to set or retrieve a
custom id to the current.
• session_id([$id]);
• session_id() is used to get or set the session id for
the current session.
<html>
<head>
<title>Setting up a PHP session</title> output
</head>
<body>. Session Id:
b9t3gprjtl35ms4sm937hj7s30
<?php
//Starting the session
session_start();
$id = session_id();
print("Session Id: ".$id);
?>
</body>
</html>
PHP - Passing Session IDs
in Query Strings
<?php session_start() ?>
<a href="myscript.php? <?php echo SID; ?>"> Home
page </a>
o/p
<a href="myscript.php?
PHPSESSID=b1231231231231231231231231231231"
>Home page </a>
• When the user clicks the link to view myscript.php,
the PHPSESSID query string value is automatically
picked up by the PHP engine and the session data is
made available to the script.
• You need to have called session_start() before
trying to access the SID constant.
• Passing session IDs in URLs is best avoided if
possible.
session vs cookies

You might also like