Jump to content

Recommended Posts

Hi guys, have a little problem here, when you login first it seems to work fine, and redirects back to index.php showing your username etc, but then when I click on another page which checks if session is set, it asks me to login again, so I do that, get redirected back to index.php again and click on the same page and then it works.. but almost every time it asks me to login twice.. and not sure why?

 

Logging in / creating the session :

<?php
// Login
  session_start();
  $_SESSION['username'] = htmlspecialchars($username); // htmlspecialchars() sanitises XSS

header( 'Location: index.php' ) ;
?>

 

then on index.php:

 

<?php
include 'connect.php';
session_start();
$username = $_SESSION['username'];
?>

 

And on another page:

 

<?php
include 'connect.php';
include_once "markdown.php";
session_start();
if(!isset($_SESSION['username']))
{
//code
}
?>

Link to comment
https://forums.phpfreaks.com/topic/260265-logging-in-problems-need-to-login-twice/
Share on other sites

Shouldnt "session_start();" be at the top of the page? before any includes.

Try that!

 

<?php
session_start();
include('connect.php');
$username = $_SESSION['username'];
?>

 

And if you are including pages that also have session start on them remove session start you will only need it once!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.