/    Sign up×
Community /Pin to ProfileBookmark

header redirection problem

I have a simple little form & script password check experiment happening.
In the file that checks the username and password, I have an if ( ) checking the two values against comparison variables that were obtained through form posted values from another file and then depending on wether the username && password are correctly verified I want a header ( ) command to redirect the page accordingly.

I use the following code:

if ($user_id == $user_chk && $pass_id == $pass_chk)
{
header (“Location: logged_in.php”);
}

else
{
header (“Location: failed.php”);
}

and I keep getting an error saying something along the lines of:

Warning: Cannot add header information – headers already sent by (output started at

etc. etc. etc.

Could somebody please tell me what I need to do differently?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 01.2005 — [font=trebuchet ms]You can't use any header functions (redirect, cookies, session start, or anything like that) after HTML has been outputted to the browser (unless you use the [url=http://www.php.net/ob_start]ob_start[/url] and related functions). Basically what I mean is, you can't have [i]any[/i] HTML output until you're done doing all checks for redirects and et cetera.[/font]
Copy linkTweet thisAlerts:
@patenaudematMar 01.2005 — [B]Update:[/B] Whoops, sorry, I started posting before yours came up, Jona...

You need to make sure you send the headers before any HTML code... therefore, even this
[code=php]<HTML>
<?php
blah blah
?>
[/code]

Would cause problems. Put the PHP code before any other code in the page to prevent this problem:
[code=php]
<?php
validating code
header('Location: blah.php');
?>
<HTML>
Etc.
[/code]

Hope it helps!
Copy linkTweet thisAlerts:
@T-WagauthorMar 01.2005 — ooohhhhhh, kewl. but strange. Thanks Jona & Patenaudemat , I'll try that tommorow at work! ?
Copy linkTweet thisAlerts:
@WedvichMar 02.2005 — I think this would work:

[code=php]if ($user_id == $user_chk && $pass_id == $pass_chk)
{
echo('<meta http-equiv="refresh" content="0;URL=logged_in.php">');
}

else
{
echo('<meta http-equiv="refresh" content="0;URL=failed.php">');
}[/code]


I use that meta output in my login system, and it works great.
Copy linkTweet thisAlerts:
@patenaudematMar 02.2005 — Yeah, that does work, and you can put it any time during the <HEAD> section. It just seems like (and no offence Wedvich) that it takes more lines of code to do the same thing.

However, if you wanted a delay before redirection to display like a "Success" or "Failure" and then "You are now being redirected...", use this method, since you can set the ammount of time it takes before redirecting.
Copy linkTweet thisAlerts:
@T-WagauthorMar 02.2005 — actually I was just wondering that, I updated the verify page to reflecte the header line being above all html and also put in a "Verifying login information..." line between the body tags, hoping there would be a second long delay or more. But that clarifies that,

Thanks wedvich and patenaudemat! ?
Copy linkTweet thisAlerts:
@T-WagauthorMar 02.2005 — I'm doing something wrong, the check is succesful because I see the proper url in the meta line however the page never redirects, here's my code:

<?php

// Getting POST data and initializing input variables

$user_id = $_POST['user_id'];

$pass_id = $_
POST['pass_id'];

// Declaring verification variables

$user_chk = "Tim";

$pass_chk = "Meggy";

// Login verification

if ($user_id == $user_chk && $pass_id == $pass_chk)

{

echo ('<head><meta equiv="refresh" content="3;URL=logged_in.php">');

}

else

{

echo ('<head><meta equiv="refresh" content="3;URL=ogged_fail.php">');

}

?>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body bgcolor="#FFFFFF" text="#000000">

Verifiying login information...

</body>

</html>
Copy linkTweet thisAlerts:
@JonaMar 02.2005 — [font=trebuchet ms]The W3C doesn't like META refreshes. If you want to automatically redirect after a specified amount of tmie, you should use JavaScript. Those without JavaScript will have to click a link to the homepage or elsewhere.[/font]
Copy linkTweet thisAlerts:
@T-WagauthorMar 02.2005 — hrm, interesting, thanks Jona
Copy linkTweet thisAlerts:
@JonaMar 02.2005 — [i]Originally posted by T-Wag [/i]

[B]hrm, interesting, thanks Jona [/B][/QUOTE]


[font=trebuchet ms]No problem. I find it interesting how they "took back" their own specification, in this particular case. Actually what's more interesting is why they decided to create it in the first place. One can quite easily see that periodic, non-stoppable refreshes could make a site inaccessible or at the very least difficult to use.[/font]
Copy linkTweet thisAlerts:
@patenaudematMar 02.2005 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]No problem. I find it interesting how they "took back" their own specification, in this particular case. Actually what's more interesting is why they decided to create it in the first place. One can quite easily see that periodic, non-stoppable refreshes could make a site inaccessible or at the very least difficult to use.[/font] [/B][/QUOTE]

Don't you love standards? Every few years, they totally change the way you design your sites and programs, and they give people an excuse to insult code. And of course, there are so many standards to choose from!

(That was all sarcasm, in case anyone was wondering... ?)
Copy linkTweet thisAlerts:
@JonaMar 02.2005 — [i]Originally posted by patenaudemat [/i]

[B]Don't you love standards? Every few years, they totally change the way you design your sites and programs, and they give people an excuse to insult code. And of course, there are so many standards to choose from![/B][/QUOTE]


[font=trebuchet ms]The best part, though, is where you find that when one thing doesn't work or is inaccessible, you can usually accomplish the same thing or something very similar in a completely different way and keep within the standardization boundaries. ? [/font]
Copy linkTweet thisAlerts:
@patenaudematMar 02.2005 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]The best part, though, is where you find that when one thing doesn't work or is inaccessible, you can usually accomplish the same thing or something very similar in a completely different way and keep within the standardization boundaries. ? [/font] [/B][/QUOTE]

I know exactly what you mean... sometimes, IE messes up CSS and DHTML and stuff, so I "change" a few things. I ask someone to take a look at the code, and they'll say something like "That's really messed up... it's 'bad CSS.'" Yeah, well maybe it is, but it validates... does that mean the standard's messed up? :eek:

Nah, impossible... ?

(By the way, T-Wag, the problem with your code was that you have <META EQUIV=... instead of <META HTTP-EQUIV=... of course you're probably looking for another way to do things now, after all of this)
×

Success!

Help @T-Wag spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...