Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Bit of help with sessions and keeping the user logged in until logged out.

Featured Replies

Hello all, yes its that time again. I have embarked on another attempt to further my knowledge and unfortunately, you guys face my questions.

 

So, i have finished making the registration / login script. Which works as i wish.

 

Here is what i am trying to achieve.

 

- User logs in and is shown new content on the page. With this, the login bar in the header changes to a nav bar.

 

- I also want it so that no matter what happens, they wont see the login bar again until they choose to log out.

 

So, this is my login.php:

 

<?php 
if (isset($_POST['loginbutton'])) {
session_start(); // Start a new session
require("conn.php");


// Get the data passed from the form
$username = $_POST['username'];
$password = $_POST['password'];

// Do some basic sanitizing
$username = stripslashes($username);
$password = stripslashes($password);

$sql = "select * from memberinfo where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );

if (mysql_num_rows($result) > 0) {
$_SESSION['loggedIn'] = true;
        echo "<script>location.href='profile.php'</script>";
} else {
$_SESSION['loggedIn'] = false;
        echo "<script>location.href='loginerror.php'</script>";
}
}
?>

 

then this is in my header:

 

<div id="headerwrap">

	<div id="logo">Social Network BETA</div>

       <?php

       if ($_SESSION['loggedIn'] = "false") {

		include 'includes/loginform.php';

	}

	elseif ($_SESSION['loggedIn'] = "true") {

	include 'includes/usermenu.php';

	}

		?> 

</div>

 

So as of now, if you login, the page refreshes, but i still see the login form.

 

Any help on what i wish to achieve / point me to some info.

 

Many thanks to all that have helped me so far!

I'm not too sure here as your code is abit vague, but if you're storing the session variables as a boolean shouldn't you also be checking them as a boolean?

 

Also you don't need elseif in situations like this as there are only 2 possible answers.

 

<div id="headerwrap">

               <div id="logo">Social Network BETA</div>

       <?php

       if ($_SESSION['loggedIn'] == false) {

                       include 'includes/loginform.php';

               }

               else  {

               include 'includes/usermenu.php';

               }

                       ?> 

       </div>

Also learn to use shorthand, see how much cleaner that is?

<div id="headerwrap">

               <div id="logo">Social Network BETA</div>

       <?php include ($_SESSION['loggedIn'] == false ? 'includes/loginform.php' : 'includes/usermenu.php'); ?> 

       </div>

Also are both codes in the same file?

 

If not don't forget to include session_start(); at the beginning of all pages you're using sessions.

Edited by Samus

  • Author

Got it working :)

 

Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php.

 

if there is a session, it gets redirected to the profile page, if not, then it stays on the same page.

 

this works with:

 

<?php
       session_start();
       if ($_SESSION['loggedIn'] == true) {

	echo "<script>location.href='profile.php'</script>";

       }

       else  {


       }
?>

 

when i put it into my index.php page. However when logging out, i get the

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

 

error... im guessing this is because my header has the session start as well...

 

i did a bit of research but still not quite sure whats causes this error. It all still works fine though.

 

One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable?

Got it working :)

 

Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php.

 

if there is a session, it gets redirected to the profile page, if not, then it stays on the same page.

 

this works with:

 

<?php
       session_start();
       if ($_SESSION['loggedIn'] == true) {

	echo "<script>location.href='profile.php'</script>";

       }

       else  {


       }
?>

 

when i put it into my index.php page. However when logging out, i get the

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

 

error... im guessing this is because my header has the session start as well...

 

i did a bit of research but still not quite sure whats causes this error. It all still works fine though.

 

One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable?

 

it means ur sending output before your calling session_start(); the session_start needs to be at the top of the page before any html or php is outputted

  • 3 weeks later...

Got it working :)

 

Just a quick one, i want to do a check if there is a session exsiting when the user visits the index.php.

 

if there is a session, it gets redirected to the profile page, if not, then it stays on the same page.

 

this works with:

 

<?php
       session_start();
       if ($_SESSION['loggedIn'] == true) {

	echo "<script>location.href='profile.php'</script>";

       }

       else  {


       }
?>

 

when i put it into my index.php page. However when logging out, i get the

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

 

error... im guessing this is because my header has the session start as well...

 

i did a bit of research but still not quite sure whats causes this error. It all still works fine though.

 

One last thing, whats the proper way to 'do nothing' in an if statement? or is leaving it blank acceptable?

 

 

Think it would be better to use php header too instead of javascript to redirect the user.

 

Like so:

 

<?php
       session_start();
       if ($_SESSION['loggedIn'] == true) {

	header("location: profile.php");

       }

       else  {


       }
?>

Think it would be better to use php header too instead of javascript to redirect the user.

 

Like so:

 

<?php
       session_start();
       if ($_SESSION['loggedIn'] == true) {

	header("location: profile.php");

       }

       else  {


       }
?>

 

ur header redirect should have an exit(); below it to be sure the rest of the script cant run incase the redirect fails for some reason

ur header redirect should have an exit(); below it to be sure the rest of the script cant run incase the redirect fails for some reason

 

 

Yes you are correct :).

Surely that's down to poor planning from the developer. If a page needs to redirect, it's usually for a good reason, hence the exit.

um yea obviously that's why i mentioned he should add the exit lol

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.