Forum Moderators: coopster

Message Too Old, No Replies

PHP login script won't redirect

Please review script and help

         

survivor12006

6:07 pm on May 30, 2008 (gmt 0)

10+ Year Member



Hi, I have a login php script that wont work, more specifically, redirect to the home page after accepting the the username and password. The page just goes blank afterward. I have been trying to work with this script for like a month and still can't get it to work right. Please, please help!

This is my registration php script:

<html>
<head>

<title>untitled</title>

</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">

<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());

//This code runs if the form has been submitted
if (isset($_POST['submit'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['username'] ¦ !$_POST['pass'] ¦ !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM Users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}

// now we insert it into the database
$insert = "INSERT INTO Users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
?>

<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.<a href=login.php>Click Here to Login</a></p>

<?php
}
else
{
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>

<?php
}
?>

</body>
</html>

This is my login script:

<html>
<head>

<title>untitled</title>

</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">

<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header('Location: home.html') ;
}
}
}

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

// makes sure they filled it in
if(!$_POST['username'] ¦ !$_POST['pass']) {
die('You did not fill in a required field.');

}

// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($checkusername);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=reg.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{

// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);

//then redirect them to the members area
header('Location: home.html') ;
}
}
}
else
{

// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?>

</body>
</html>

I think I can't redirect to the home page in a php script that has html tags? So how do I this? Please review my script and tell whats wrong? Thanks!

[edited by: dreamcatcher at 9:53 pm (utc) on May 31, 2008]
[edit reason] use example.com. Thanks. [/edit]

alex95_bg

8:52 pm on May 30, 2008 (gmt 0)

10+ Year Member



Does it work with a meta tag?
Have you tried echo("<meta ..") ;
In place of header('Location: home.html') ;

londrum

8:58 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i think you need to include the absolute url, rather than a relative one.

try changing this

header('Location: home.html');

to something like this

header('Location: http://www.example.com/home.html');

survivor12006

9:50 pm on May 30, 2008 (gmt 0)

10+ Year Member



Please bare with me, I am freshly new at this so srry in advance if I do any of this wrong. Like I said it took me like a month to figure what I have right :-). Anywho, I tried the:
echo "<meta http-equiv="Refresh" content="0; url=http://www.example.com/home">";
and it resulted in a blank white screen rather than the usual blank custom screen.

and I tried the:
header('Location: http://www.example.com/home.html');
and it produced the usual blank custom screen

So those two didn't work..but thanks. Are there any other suggestions, please?!Does anyone think the registration and login code looks ok? Maybe its another line of the script thats the problem? Idk....

dreamcatcher

6:24 am on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your error logs and see if anything is there. Also, set your error reporting level to E_ALL. You are using the header function after you have outputted HTML to the browser and this isn`t allowed. The header function simply won`t work.

[uk.php.net...]

Try moving your code around so that the PHP processing occurs before you send the HTML to the browser.

dc

xiongbin

11:00 am on May 31, 2008 (gmt 0)

10+ Year Member



function redirect($url){
echo "<script>location.href=\"$url\"</script>"
}

use the function ..

survivor12006

9:34 pm on May 31, 2008 (gmt 0)

10+ Year Member



When you say switch the login script around do you mean like this where the html tags are last as in the script below instead of first as in the script above:

<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header('Location: http://www.example.com/home.html');

}
}
}

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

// makes sure they filled it in
if(!$_POST['username'] ¦ !$_POST['pass']) {
die('You did not fill in a required field.');

}

// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($checkusername);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=reg.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{

// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);

//then redirect them to the members area
header('Location: http://www.example.com/home.html');
}
}
}
else
{

// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?>
<html>
<head>

<title>Example.com-Spotting good music & leaving all nonsense behind</title>

</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">
</body>
</html>

I tried and this also didn't work..it also resulted in a blank custom page...

[edited by: dreamcatcher at 9:52 pm (utc) on May 31, 2008]
[edit reason] use example.com. Thanks. [/edit]

dreamcatcher

9:55 pm on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please try not to post your url in your messages. What about settings your error reporting? After your opening <?php tag add the following:

error_reporting(E_ALL);

See if that generates an error.

dc

survivor12006

10:04 pm on May 31, 2008 (gmt 0)

10+ Year Member



Like I said I'm really new at this so I don't how to check my error log.ha ha.. but I know I'm being hosted by yahoo so with a push on a button the files automatically uploaded to the server..ya know? and I couldn't see any files containing the error log and when I log into phpmyadmin I see error #1146 saying the backup file it was checking doesn't exist but I doubt that has anything to do with anything...Idk, I am totally stuck..

survivor12006

10:17 pm on May 31, 2008 (gmt 0)

10+ Year Member



yeah, everytime I try the echo function it results in a blank white page..

survivor12006

11:46 pm on May 31, 2008 (gmt 0)

10+ Year Member



So yeah, I think I really messed up :-( when I reviewed and made a few changes so now whenever I try to login with the correct information I get the incorrect password error. *sigh*!

dreamcatcher

6:53 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line won`t run correctly:

if(!$_POST['username'] ¦ !$_POST['pass']) {

Should be two pipes:

if(!$_POST['username'] ¦¦ !$_POST['pass']) {

dc

FourDegreez

5:02 pm on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should supply the proper HTTP status code in addition to the location header:

header("HTTP/1.1 302 Found");

survivor12006

5:15 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



k thanks, I changed it to this:
if(!$_POST['username'] ¦¦ !$_POST['pass']) {
and it's still saying incorrect password after I typed in the correct info.

survivor12006

7:09 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



thanks, added the status code in addition to location header still results in incorrect password when I run the script.