I have been learning PHP for a little bit now, and it has been going really easy for the most part. The only thing I'm hung up on is getting sessions to work. Google has been unforgiving in this endeavor.
It could be one of two reasons; syntax or my software. I'm currently building a local website using EasyPHP 5.3.5.0 on a machine that isn't connected to the internet. Connecting it to the internet is not an option.
What I currently know of sessions is that a lot of syntax related to it has be deprecated, replaced by the superglobal $_SESSION
array, which is a lot easier to use. start_session();
must be before any syntax relating to sessions. However, my login script isn't establishing a session, as a quick !isset ($_SESSION['username'])
always returns true.
My script is set up like this:
PHP include to login.php, which is a form. check_login.php is what validates it, and if a query returns one row, it'll redirect to login_success.php which establishes the session, gives a welcome message then redirects (Using JavaScript) to the homepage.
Any ideas?
EDIT to include more information:
Here is a synopsis of my code:
index.php:
include 'main_login.php';
main_login.php:
if(!isset ($_SESSION['username'])){ ... Login form, action="cehcklogin.php" method="post" ... }else{ var_dump ($_SESSION): // Just to see if it works }
checklogin.php:
Connect to SQL $username = $_POST['username']; $password = $_POST['password']; $username / $password stripslashes / mysql_real_escape_string Query to find the username & password $count = mysql_num_rows($result); if($count = 1){ $_SESSION["username"] = $username; $_SESSION["password"] = $password; header("location:login_success.php"); }else{ echo "Wrong Username or Password." }
login_success.php:
The login process goes to all of the way here, redirects home and that's where the problem is.
session_start(); var_dump($_SESSION); //This works if(!isset ($_SESSION['username'])){ header("location:index.php"); } Javascript redirect, and a welcome message appears.
It all works until you get to the homepage, which $_SESSION['username']
should be set, and it should not display the form, but it does.
$_SESSION['username']
? What does your login code look like, and how have you made sure that the login is actually succeeding? Also check yoursession.save_path
: php.net/manual/en/… --see that it's set correctly and check if the file is being created and what its contents are. – Lèse majesté Jan 27 '11 at 10:40