Jump to content

issues with header() and cookies.


walkonet

Recommended Posts

ok... I made a login page that is supposed to test the email and password of a registered user. once that is accomplished it is supposed to redirect the user to the loggedin.php page which it does not. however if you type in loggedin.php after you enter in the required info it print out perfectly???

my site if you would like to see the problem in action is [url=http://walkonet.com/test/login.php]http://walkonet.com/test/login.php[/url]
test acount info: asd@asd.com/clown123 hit ok and then in the URL type in loggedin.php and see the result...

here the login script:
[code]
<?php # user login - login.php
//send nothing to the browser befor the setcookie() lines

function escape_data ($data) {
global $dbc; //Need the connection
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data),$dbc);
} // End of Function

//check if the form has been submitted
if(isset($_POST['submitted'])) {


require_once('./connect/mysql_connect.php');

$errors = array();

//Check for an email.
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter an E-mail address.';
} else {
$e = escape_data($_POST['email']);
}

//Check for an email.
if (empty($_POST['password'])) {
$errors[] = 'You forgot to enter a password.';
} else {
$p = escape_data($_POST['password']);
}

if(empty($errors)) {//<-- its ok

/*retrive the user id and first name for
that email/passord combonation */
$query = "SELECT user_id, first_name FROM users WHERE email='$e' AND password=SHA('$p')";
$result = @mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM); //return a record, if applicable

if($row){ //a record was pulled from the database.

//set cookies and redirect.
setcookie('user_id', $row[0]);
setcookie('first_name', $row[1]);

//redirct user to logged in page. loggedin.php
//start defining the URL
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
//check for trailing slash.
if((substr($url, -1) == '/') or (substr($url, -1) == '\\')) {
$url = substr($url, 0, -1); //chop off the slash
    }
//add the page.
$url .='/loggedin.php';

} else { //no record was found
$errors[] ='The E-mail/Password combonation does not match those on file.';
//public message
$errors[] = mysql_error() . '<br /><br />Query: ' . $query; //debug msg
}

} //end of if (empty($errors)) IF


mysql_close();

} else {//form has not been submitted

$errors = NULL;

} //end of main submit conditional

//begin the page now.
$page_title = 'Login';
include('./includes/header.html');

if(!empty($errors)) { //print all errors
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach($errors as $msg) { //print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}

//create the form
?>
<h2>Login</h2>
<form action="login.php" method="post">
<p>E-mail Address: <input type="text" name="email" size="20" maxlength="40" /></p>
<p>Password: <input type="text" name="password" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Login" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include('./includes/footer.html');
?>

[/code]

and the loggedin.php script

[code]
<?php #logged in page - loggedin.php
#user has redirected from login.php

//if no cookies are present, redirect the user.
if(!isset($_COOKIE['user_id'])) {

//start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

//Check for trailing slash
if((substr($url, -1) == '/') or (substr($url, -1) == '\\')) {
$url = substr($url, 0, -1); //chop off the slash
}

//add the page.
$url .= '/index.php';
header("Location: $url");
exit();
}

//Set the page title and include html header.
$page_title = 'Logged In!';
include('./includes/header.html');

//Print a customized messages.
echo "<h1>Logged In!</h1>
<p>You are now logged in, {$_COOKIE['first_name']}!</p><p><br /><br /></p>";

include('./includes/footer.html');
?>
[/code]

any ideas?
Link to comment
Share on other sites

login.php does not have any header redirects when the user logs in successfully. I see you setup the url but you dont use a function to perform the redirect to loggedin.php:

[code=php:0]if($row){ //a record was pulled from the database.

//set cookies and redirect.
setcookie('user_id', $row[0]);
setcookie('first_name', $row[1]);

//redirct user to logged in page. loggedin.php
//start defining the URL
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
//check for trailing slash.
if((substr($url, -1) == '/') or (substr($url, -1) == '\\')) {
$url = substr($url, 0, -1); //chop off the slash
    }
//add the page.
$url .='/loggedin.php';[/code]


This wil do fine:
[code=php:0]if($row){ //a record was pulled from the database.

//set cookies and redirect.
setcookie('user_id', $row[0]);
setcookie('first_name', $row[1]);

//redirct user to logged in page. loggedin.php
header("Location: loggedin.php");
                  }[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.