Simple Login Script: Problems with header(Location)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
OU_Student
Forum Newbie
Posts: 7
Joined: Tue Feb 10, 2009 5:30 pm

Simple Login Script: Problems with header(Location)

Post by OU_Student »

Hello,

I'm writing a simple login verification script and everytime I run the file I'm getting 404 not found. I'm not sure if ../ is allowed in the location header.

I would appreciate any feedback or help.

Thanks 8)

Code: Select all

<?php
session_start();
//Get user input.
$userName = $_POST['userName'];
$userPassword = $_POST['userPassword'];
 
//Database connection
 
$dbc=mysql_connect ("localhost", "my_username","my_password") or die('Cannot connect to the database because: ' . mysql_error());
 
$Link = mysql_select_db ("my_database");
    
$query = 'SELECT user_name,password FROM users '
 
        . ' WHERE user_name = \''.$userName.'\' AND password = \''.$userPassword.'\'';
  
 
    $result = mysql_query($query) or die(mysql_error());
 
    while($row = mysql_fetch_array($result))
    {
        $user_name = $row['user_name'];
        $password = $row['password'];
    }
 
//Log in OK?
if ( $userName == $user_name && $userPassword == $password) {
    $_SESSION['logged in'] = 'ok';
    $_SESSION['user name'] = $userName;
    header([color=#FF4040]'../Location: userloggedin/index.php'[/color]);
    exit();
}
//Jump to bad login.
header([color=#FF0000]'Location: ../index.php'[/color]);
exit();
 
?>
Last edited by OU_Student on Fri Feb 20, 2009 2:05 pm, edited 1 time in total.
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: Simple Login Script: Problems with header(Location)

Post by p3rk5 »

Before any code on the page, including <html> tags, you must put this when using sessions:

Code: Select all

<?php session_start(); ?>
OU_Student
Forum Newbie
Posts: 7
Joined: Tue Feb 10, 2009 5:30 pm

Re: Simple Login Script: Problems with header(Location)

Post by OU_Student »

I used this as a require file for my pages and I do have session_start(); before all of the html. My problem is the header('Location: ../index.php'); .. I'm not sure if the the way i'm switching directories is allowed.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Simple Login Script: Problems with header(Location)

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
OU_Student
Forum Newbie
Posts: 7
Joined: Tue Feb 10, 2009 5:30 pm

Re: Simple Login Script: Problems with header(Location)

Post by OU_Student »

Sorry about that. If anyone has any suggestions to my code highlighted in red, I would appreciate it =)
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Simple Login Script: Problems with header(Location)

Post by watson516 »

Code: Select all

header('Location: ../index.php');
This works for me.
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: Simple Login Script: Problems with header(Location)

Post by p3rk5 »

Is

Code: Select all

header('../Location: userloggedin/index.php');
supposed to be

Code: Select all

header('Location: ../userloggedin/index.php');
or perhaps

Code: Select all

header('Location: userloggedin/index.php');
Post Reply