1

I am working on a login system for a client and I have run into an issue that I am unsure how to fix. I have it set so that when the user tries to access the index.php of a certain directory they are require to login. The code that I have doing this is:

<?php
   session_start();
   if(!$_SESSION['myusername']){
      header("location:Login_admin.php");
   }
?>

On the Login_admin.php page, there is a form with the action="checklogin.php". here is where it connects to the database and starts a session where it stores the username:

if($count==1){
   session_start();
   // Register $myusername, $mypassword and redirect to file "login_success.php"
   $_SESSION['myusername'];
   $_SESSION['mypassword']; 
   header("location:index.php");
}

Originally the above code did not have the session_start() so I added that in in attempt to solve my problem.

The actual problem is the session is not registering. If I go to one of the other pages in the director (all of them have that first segment of code at teh top) and log in it sends me to index.php like I want, but when I try to go back to that page it makes me log in again. At one point I had that first segment of code on my index page and even with the correct log in it would loop back to the login page.

I was shown this script by a friend and have not really changed much. Originally the script had:

session_register("myusername");

But after some debugging, I changed it to:

$_SESSION['myusername'];

A final note, I am not an expert the issue is probably considered a silly mistake but for the life of me I can't figure it out.

Thanks in advance!

1
  • Both of the answers below helped me solve the issue. Thanks guys! Commented Mar 31, 2012 at 17:18

2 Answers 2

1

the

$_SESSION['myusername'];

line does absolutely nothing
to assign a value to session variable you have to assign a value to session variable
the usual way values being assigned to variables in PHP

Sign up to request clarification or add additional context in comments.

1 Comment

The correct way would be to do: $_SESSION['myusername']=$myusername; ?
1

You have to give a value to each session variable

$_SESSION['myusername'] = 'thename';
$_SESSION['password'] = '*******';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.