Forum Moderators: coopster

Message Too Old, No Replies

An alternative to the header() function

it's screwing up my format...

         

freshrod

3:34 am on May 19, 2006 (gmt 0)

10+ Year Member



In part of my user registration I have a php file that checks to see if the user has been activated. If so, it uses the header() function, to send them on to the next page, but if not, prints a error message.

<?php
$sql = mysql_query
("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");

$login_check = mysql_num_rows($sql);

if($login_check > 0) {
$url="http://www.freshrod.com/MHS/MHS1986login_success.php";
header("Location: $url");
} else {
echo "<font color='red'>You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
Please try again!</font><br />";
echo "<br /><a href=\"MHS1986login.html\">Back to Login Page</a>";
}
?>

then the page html is down here...

I know the header() has to appear before anything else is printed to the browser, but I don't like the way the error message appears outside (above at the top of the page) the html. Is there a way around this?

dreamcatcher

7:08 am on May 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi freshrod,

You can use a meta refresh if you want to:


function refresh($url)
{
?>
<html>
<head>
<title>Please wait..</title>
<meta http-equiv="refresh" content="2; url=<?php echo $url;?>">
</head>

<body>
Login successful...please wait..
</body>
</html>
<?php
}

and then..


if($login_check > 0) {
$url="http://www.freshrod.com/MHS/MHS1986login_success.php";
refresh($url);
exit;
}

Set the refresh rate to 0 if you want no message.

dc

omoutop

8:16 am on May 19, 2006 (gmt 0)

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



Alternative you may put your error message in a string, something like $error_msg = "My error message goes here";

Later in the htm file of yours, just do a simple check and echo your message

<?
if ($error_msg!="")
{
echo $error_msg;
}
?>

ergophobe

4:55 pm on May 19, 2006 (gmt 0)

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



Or just turn on output buffering

[php.net...]

freshrod

3:37 am on May 20, 2006 (gmt 0)

10+ Year Member



Thanks everyone for the suggestions.

I think I will use the string because it seems to fit my purpose best, and is most flexible.

That... and I understand it best...lol