Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sessions & session variables in a PHP Login Script

Tags:

php

session

login

I have just finished creating an entire login and register systsem in PHP, but my problem is I haven't used any sessions yet. I'm kind of a newbie in PHP and I've never used sessions before. What I want to do is, after the user registers and fills out the login form, they will still stay on the same page. So, there will be one part of the which will be if the session is logged_in and the other part will be else (the user is not logged in so display the login form). Can anyone tell me how to get started?

like image 537
Penian4 Avatar asked Apr 10 '12 23:04

Penian4


People also ask

What is the use of sessions?

Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.

What is session with example?

The definition of a session is a meeting, series of meetings or school term. An example of a session is jury members meeting to agree on a verdict. An example of a session is the time when students are attending classes at school.

What is session and how it works?

A session is a group of user interactions with your website that take place within a given time frame. For example a single session can contain multiple page views, events, social interactions, and ecommerce transactions. Learn more about the different request types in Analytics.

What are sessions used for in PHP?

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.


1 Answers

Hope this helps :)

begins the session, you need to say this at the top of a page or before you call session code

 session_start();  

put a user id in the session to track who is logged in

 $_SESSION['user'] = $user_id; 

Check if someone is logged in

 if (isset($_SESSION['user'])) {    // logged in  } else {    // not logged in  } 

Find the logged in user ID

$_SESSION['user'] 

So on your page

 <?php  session_start();    if (isset($_SESSION['user'])) {  ?>    logged in HTML and code here  <?php   } else {    ?>    Not logged in HTML and code here    <?php  } 
like image 65
Ross Avatar answered Sep 22 '22 23:09

Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!