You are currently viewing PHP Tutorial: Sessions, Cookies, and FTP
Session, Cookies, and FTP

PHP Tutorial: Sessions, Cookies, and FTP

Hello PHP Programmers, again, your most welcome in this PHP tutorial series to getting more points in detail. In this PHP post, we are going to learn about Session, Cookies, FTP and we will also know about How to set Cookies, Session in our PHP program. We are also knowing that the Internet is a stateless place, we can not store any type of data over the Internet or Web page. By refreshing the web page, a new page will be initialized, load, and displayed.

For Example – Without storing the Cookies and Sessions, every time, we got a new initialized page, that means, if we set the background color of the web page dynamically, we can not see that background color, after refreshing that web page. This happens because we are not storing the background color data in anywhere.

One more example– If we want to visit the particular website page again with previous saved work that have performed by us. So, we have to allow the site cookies to store the previous data in the textual form on our machine or can be store on Server.

So, let’s begin our journey towards the Cookies, Sessions, and FTP protocol. We will study step by step about Cookies & Sessions and we will also learn how to create and run our PHP scripts.

First, we will know about How to set, read, delete, setting expiration time of web Cookies.

After learn Cookies, we will jump to the FTP mechanism. How to –

  • Work with FTP.
  • Read a Remote directory with FTP.
  • Download files with FTP.
  • Upload files with FTP.
  • Delete files with FTP.
  • Create and remove the directories with FTP.

After then, we will jump to the next topic The Session.

Ok, PHP Programmers, Let’s get started –

How to read, delete, set expiration time of Web Cookies?

By the help of PHP script or PHP coding, we can do various tasks for setting the Web Cookies in your web page. we have knew about cookies –

for more info in Cookie – Cookies in PHP.

How to set the Web Cookies?

Don’t worry, it is very easy, no more coding required, no more steps are required to set the Web Cookies. Only one pre-defined function are used to set the Web Cookies within your PHP Web Page. you can use it in another program to look for the stored cookies data. you can also see the changes according to saved cookies on your web page.

For setting the Cookies on your webpage, you have to use the setcookie(value1,value2,value3,..) PHP pre-defined function.

You must put this setcookie() function in your PHP code for set the Cookie.

Syntax – setcookie(name,value,expire,path,domain,secure)

Arguments in detail

  • name : The name of the cookie.
  • value : The value of the cookie. (This value is stored on the client’s computer, so do not store sensitive information.)
  • expire : The time the cookie expires. This is the number of seconds since January 1, 1970. You’ll most likely set this with PHP time function plus the number of seconds before you want it to expire.
  • path: The path on the server in which the cookie will be available.
  • domain : The domain for which the cookie is available.
  • secure : Indicates that the cookie should only be transmitted over a secure HTTPS connection. When set to 1, the cookie will only be set if a secure connection exists. The default is 0.

Example: Q. How to set the Web Cookies through your PHP program?

<!DOCTYPE html>
<html>
<head>
<title>
Set the Cookies
</title>
</head>
<body bgcolor="lightblue">
<h1>Set the Cookie</h1>
<?php
echo "Hello, PHP Programmers<br><br><br>";
setcookie("color","red");
echo "Cookie has been set successfully!!!";
?>
</body>
</html> 

Output –

How to read the Web Cookies?

We have been successfully set the Web Cookie for Web page. So now, we can fetch the saved Cookie using $_Cookie[] array. In other words, we can read the Cookies through pre-defined $_Cookie[‘CookieName’] array.

When you set a cookie, it won’t be visible to your scripts until the next time you load a page. That’s because the cookie is sent to the server from the user’s machine, so immediately after you set a cookie, you won’t be able to read it. you need to get a page back from the browser first.

Note :- Cookies are sent in the HTTP headers in pages sent to you by the browser.

We set a cookie named color in the previous topic – and we’ll read it here. The values of the cookies are automatically loaded into the global array named $_COOKIES, much as the values of Web page data are stored in $_REQUEST, which makes this process easy.

Example – QHow to read the Cookie from the previous Web page?

<html>
<head>
<title>
Read the Cookie
</title>
</head>
<body bgcolor="lightblue">
<h1>Read the Cookie</h1>
<?php
if(isset($_COOKIE['color']))
{
echo "Cookie's value -->  ".$_COOKIE['color'];
}
else
{
echo "No Cookies are available";
}
?>
</body>
</html>

Output-

Ok, we have set and read the cookies through our PHP program or script. So now, we should know about what’s the expiration time of cookie?

Generally, Cookies are expired after you close the Web Browser Window.

But you can customize this thing, which means you can set your own expiration time for web cookies.

How to set expiration time for Web Cookies?

We can set the time for cookies will be expired or deleted from the Web browser or User’s browser. we have to specify the time to our cookies to be expired from user’s machine. No more coding required to assign the expiration time, only one method is required and that is time() function.

This (time( ) ) function represents the current time according to your country. In other words, this function fetch your system’s time value.

You can specify the time() function along with your desired second’s value. That means, from the current time, how much you want to keep the cookies on the user’s computer.

Syntax setcookie(“color”,”red”,time() + 60) // 60 is the value of seconds.

Example – Q How to set the Expiration time for the intended Cookies?

<!DOCTYPE HTML>
<html>
<head>
<title>
Set the Expiration time for Web Cookie
</title>
</head>
<body bgcolor="skyblue">
<h1>Set the Expiration Time</h1>
<?php
echo "<br><br>Cookie will expire after every 60 seconds";
setcookie("color","red",time()+60);
?>
</body>
</html>

Output –

How to delete the Web Cookies?

We can also want to delete the saved cookies in anytime. if we want to delete the cookies before expiration time, so we have to change the expiration value. PHP doesn’t provide any method or function for delete the stored cookie, but we can get a different way to delete it.

So, therefore, we can altering in the expiration time section.

To delete the stored cookie – we have to go backward from the current time. In other words, we have to change the value in negative

Example – setcookie(“color”,”red”,time() – 60);

Before set the cookie, this function indicates delete the cookie, because of its negative value in the place of expiration time argument.

Example –

<!DOCTYPE html>
<html>
<head>
<title>
Delete the Cookie
</title>
</head>
<body bgcolor="skyblue">
<h1>Example - Delete the Cookie</h1>
<?php
setcookie("color","red",time()-60);
if(isset($_COOKIE['color']))
{
echo "<br><br>Cookie is: ".$_COOKIE['color'];
}
else
{
   echo "<br>No Cookie is available!!!! Stored Cookie has been deleted";
}
?>
</body>
</html> 

Output -:


Ok, Programmers!!! This is it, for now, we will meet very soon with a remaining topic. (Sessions, FTP mechanism). Thank you so much for reading this full article. Stay tuned with us and do follow us on the social networks for more daily technical updates.


Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply