September 8, 2013

Session doesn't work

User2758781’s Question:

I’m trying to make a login page. when i try to register the username as a session it should redirect me to another page, where it checks if that session is not registered. if it isn’t, it redirects me back to the login page. well, i think it fails on the second page.

Login.php:

if($count==1){$_SESSION['user'] = $username;$_SESSION['pass'] = $password;header("location:Login_Success.php");}
else{echo "<p style='color:red'>Wrong username or password!</p>";}

login_success.php:

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

You are missing session_start(); on the begining of your code.

session_start(); //This will get you connected with your previous session

if($count==1){$_SESSION['user'] = $username;$_SESSION['pass'] = $password;header("location:Login_Success.php");}
else{echo "<p style='color:red'>Wrong username or password!</p>";}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!