views:

95

answers:

1

I'm a bit embarrassed to say, but I've run into issue with creating a PHP based login system. I'm using a site template to handle the looks of the the login process, so I will spare you the code. Here is my thought process on how to handle the login:

Create a simple login.php file. On there will be a form whose action is set to itself. It will check to see if the submit has been clicked, and if so validate to make sure the user entered a valid password / username. If they do, set a session variable save some login info (username, NOT password), and redirect them to a restricted area. If the login info isn't valid, save an error message in a session variable, display error message giving further instruction, and wait for the user to resubmit. Here is a chunk of what I have - hopefully one of you experts can see where I've gone wrong, and give me some insight:

<?php
session_start();

if(isset($_POST['submit'])) {
    if(!empty($_POST['username']) AND !empty(!$_POST['password']))
    {
        header("Location: http://www.google.com");
    } else {
        $err = 'All the fields must be filled in!';
    }
}

if($err) {
    $_SESSION['msg']['login-err'] = $err;
}
?>

Now the above is just an example - the intent of the above code is to process user input, with the script validating simply that the user has given input for username and password. If they have, I would like them, in this case, to be redirected to google.com (for the sake of this example). If not, save an error message. Given my current code, the error message will display perfectly, however if the user submits and has something entered for the username and password, the page simply doesn't redirect. I'm sure this is a silly question, but I am a beginner, and well, to be honest, a bit buzzed right now. Thanks so much!

+1  A: 

Well, without looking at the rest of your code it's difficult to say. But with headers, the header must be sent before anything is written to the response stream (ie before any echo statements OR any code that is not included inside of <?php ... ?> tags) so that could be why it isn't redirecting. The code you supplied looks good to me.

Chris Thompson
...agreed. You beat me to it:)
therealsix
That code you see there is at the VERY top of the page.The only other code that is PHP is:<?php if($_SESSION['msg']['login-err']) { // This will output the registration errors, if any ?> <div class="notification attention png_bg"> <div> <?php echo $_SESSION['msg']['login-err']; ?> </div> </div> <?php unset($_SESSION['msg']['login-err']); } else { ?> <div class="notification information png_bg"> <div> Please enter your username and password. </div> </div> <?php } ?>
Brian Lang
I'd like to post the entire page, but sadly, being a new user it won't allow me to do so (to prevent spam it says). Any other ideas? Thanks guys, I appreciate it.
Brian Lang
Went through and you are right, I didn't post this part cause I didn't want to bore you, but I had placed an echo statement that outputted a value in an area I wasn't noticing. Sorry for such a silly mistake, but thanks for your response.
Brian Lang
Not at all, I'm sure I can speak for everyone on this site when I say we've all made a few bone-heads in our past. Glad to help!
Chris Thompson