Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  11] [ 7]  / answers: 1 / hits: 16570  / 10 Years ago, wed, june 25, 2014, 12:00:00

So I am developing a login system for my website and I would like to change the navigation bar when the user is logged but I can't get it to work because the session keeps getting lost after I refresh the page to update the navigation bar. This is what I got in my login.php script:



if ($loginNumRows == 1) {
echo 'true';
session_start();
$_SESSION['username'] = $loginRow['name'];


When this echoes 'true', the following javascript is loaded:



if (html == 'true') {
window.location.href = index.php;


I tried with location.reload(true); and it is still the same. And in the main page, where the navigation bar is, I make the following check:



<?php if (isset($_SESSION))
{...}


However this always returns false. Any ideas what to do? Thanks!


More From » php

 Answers
31

First of all, no need for javascript to redirect you to the index page. You can do it from PHP too:



header(Location: index.php);


So the login.php will look like this:



if ($loginNumRows == 1) {
session_start();
$_SESSION['username'] = $loginRow['name'];
header(Location: index.php);


Note, that I have changed the order. As others I strongly recommend to put session start above all of your code.



And finally make sure that you have session started in the index.php file too(this should be also placed above all of your code ):



session_start();


EDIT: If you want to stick with your javascript method ( witch I highly not recommend ), than your login.php code should look like this:



if ($loginNumRows == 1) {
session_start();
$_SESSION['username'] = $loginRow['name'];
echo 'true';

[#70437] Monday, June 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
richardaydenc

Total Points: 148
Total Questions: 125
Total Answers: 98

Location: Seychelles
Member since Mon, Jun 28, 2021
3 Years ago
;