Using sessions session variables in a PHP Login Script

0 votes
I have created a login and register system in PHP. But I didn't use any sessions yet. 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 someone please help me with this I want to do this with the session?
Jun 17, 2022 in PHP by narikkadan
• 63,600 points
717 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Add this at the top of the page:

session_start(); 

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

 $_SESSION['user'] = $user_id;

Then check if someone has logged in:

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

Then add this 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
 }

I hope this helps you.

answered Jun 18, 2022 by Kithuzzz
• 38,000 points

edited Mar 5

Related Questions In PHP

0 votes
2 answers

How can we create a session in PHP?

Hello, niroj. Here is my idea session_start(); $_SESSION['USERNAME'] ...READ MORE

answered Dec 7, 2020 in PHP by Famous
• 140 points
1,397 views
0 votes
1 answer

How can I connect to a Tor hidden service using CURL in PHP?

Hello @kartik, I use Privoxy and cURL to scrape Tor ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
5,620 views
0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
35,641 views
0 votes
1 answer

How do I make a PATCH request in PHP using cURL?

Hello @kartik, Use this: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); should do it. Hope ...READ MORE

answered Oct 19, 2020 in PHP by Niroj
• 82,840 points
8,938 views
0 votes
1 answer

How to get the list of specific files in a directory using php?

Hello @kartik, You'll be wanting to use glob() Example: $files = ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,840 points
2,388 views
0 votes
1 answer

How to create a database using PDO in PHP?

Hello @Hitesh, Yes, you can create a database using ...READ MORE

answered Nov 9, 2020 in PHP by Niroj
• 82,840 points
6,654 views
0 votes
1 answer

"Cannot send session cache limiter - headers already sent"

The line "Headers already sent" means that ...READ MORE

answered Feb 18, 2022 in Others by Aditya
• 7,680 points
7,361 views
0 votes
0 answers

PHP Unset Session Variable

I am working on creating a product ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
1,126 views
0 votes
0 answers

Why is PHP session_destroy() not working?

Why is session_destroy() not working? Is there any other way ...READ MORE

Jun 12, 2022 in PHP by narikkadan
• 63,600 points
1,495 views
0 votes
0 answers

What is the difference between Sessions and Cookies in PHP?

What is the distinction between Sessions and Cookies ...READ MORE

Jun 13, 2022 in PHP by narikkadan
• 63,600 points
465 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP