downah Posted April 3, 2012 Share Posted April 3, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260265-logging-in-problems-need-to-login-twice/ Share on other sites More sharing options...
batwimp Posted April 3, 2012 Share Posted April 3, 2012 is there anything in markdown.php that could be destroying or resetting the session? Quote Link to comment https://forums.phpfreaks.com/topic/260265-logging-in-problems-need-to-login-twice/#findComment-1334009 Share on other sites More sharing options...
downah Posted April 3, 2012 Author Share Posted April 3, 2012 Nope, got rid of markdown and still doing the same.. Quote Link to comment https://forums.phpfreaks.com/topic/260265-logging-in-problems-need-to-login-twice/#findComment-1334065 Share on other sites More sharing options...
jayjay159357 Posted April 3, 2012 Share Posted April 3, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/260265-logging-in-problems-need-to-login-twice/#findComment-1334085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.