All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Start Session In PHP

Last Updated : Mar 11, 2024

How To Start Session In PHP

In this tutorial we will show you the solution of how to start session in PHP, as we know that session is a way with help of which we can store information in a website across multiple pages.

These are like cookies, but different from them. Cookies have specific and infinity life span whereas sessions have limited time span.

For example, they are created at time of login and destroyed when you logout.

Step By Step Guide On How To Start Session In PHP :-

Here, first we know that sessions are used to store information’s. And there are many different ways to create, access and destroy sessions in php.

In this article, today we are going to understand how to start session in php. let us see the codes given below to understand easily.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
 <head>
  <title> isset submit in php </title>
 </head>
 <body>
  <?php
   // rest code for session of php code is here
  ?>
 </body>
</html>
  1. Here, as you see that first we create a php basic structure. In which we see that <?php is the starting tag of php codes whereas ?> is the ending tag of php codes.
  2. Inside these codes as you see that we create a php session. And that is done with the help of session_start() function.
  3. session_start() function is inbuilt function in php. And it generates a unique id for user. As well as this function is also used to resume the session that we create previously for user. The difference between cookie and session is that cookie is used from client side to store data whereas session is used to store data from server side.
  4. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  5. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  6. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  7. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  8. Inside body, we can also access and destroy session that we created and check whether a session is created or not. One thing to note here that session that we want to start always started at top of page. Rest like session accessing and destroying can be done everywhere in php page.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to start session in php.

I hope this tutorial on how to start session in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪