PHP Session Help

SpaceshipSpaceship
NamecheapNamecheap
Namecheap AuctionsNamecheap Auctions
Watch

Ik

Quality //VIP Member
Impact
8
I need help!

I'm using sessions to protect some pages from unauthorized users.

Here is my code:

When there is a match in the database:

Code:
session_start();
session_register("username");
header("location:index.php");

In the protected page:

Code:
session_start();
if(!isset($_SESSION['username']))
{
	header("location:login.php");
}

The code works perfectly on my local machine, but when I upload to dreamhost.com it keeps redirecting me to the login page regardless I type the wrong credentials or the right ones. The database is fine, and the tables include the login information the I use to log in!

Any thoughts??
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Do not use session_register - that requires register_globals and long story short - it is BAD.

PHP:
<?php

session_start();

$_SESSION['username'] = $username;
$_SESSION['loggedin'] = true;

header("Location: index.php");
exit;

?>

...

PHP:
<?php

session_start();

if (!isset($_SESSION['username'], $_SESSION['loggedin']) OR $_SESSION['loggedin'] != true)
{
        header("Location: login.php");
        exit;
}

?>
 
0
Close
First thing is to verify if the $_SESSION variable is set.
This function is deprecated as of PHP6. Which version is your host using ?
 
0
Close
you are a php guru :) it works, thanks a lot

Do not use session_register - that requires register_globals and long story short - it is BAD.

PHP:
<?php

session_start();

$_SESSION['username'] = $username;
$_SESSION['loggedin'] = true;

header("Location: index.php");
exit;

?>

...

PHP:
<?php

session_start();

if (!isset($_SESSION['username'], $_SESSION['loggedin']) OR $_SESSION['loggedin'] != true)
{
        header("Location: login.php");
        exit;
}

?>


---------- Post added at 05:25 PM ---------- Previous post was at 05:21 PM ----------

First thing is to verify if the $_SESSION variable is set.
This function is deprecated as of PHP6. Which version is your host using ?

Thanks :)
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back