PHP Redirect

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
mjstehn
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2004 4:25 pm

PHP Redirect

Post by mjstehn »

I have a form that people fill out and then it calls a page where the data is processed and a confirmation page is displayed. This is a weekly submission, so what is happening is that users are coming to the page that processes the data because there browser remembers this page. I would like to check the $_POST variable and if blank direct them back to the entry form. Can anyone help me go about this? I have tried using header and put it before any data gets sent to the browser, but it does not seem to work. Thanks,
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Redirect

Post by Celauran »

Can you elaborate on what you've tried and what isn't working? What errors are you getting?

This should work fine:

Code: Select all

if (empty($_POST))
{
    header("Location: whatever.php");
}
mjstehn
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2004 4:25 pm

Re: PHP Redirect

Post by mjstehn »

It just pulls the process data page with empty results. I even added an echo statement to make sure that the $_POST was empty and the that if condition is met. It is. It just does not re-direct. Where should I put this statement, before <html>? It is now after <title>. I would think there should be no issues with this working. Thanks for the help.

Mike
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Redirect

Post by Celauran »

It needs to be at the very top of the page, before any HTML.
mjstehn
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2004 4:25 pm

Re: PHP Redirect

Post by mjstehn »

Still does not work. Here is the code. The In If Statement comes up and then the rest of the page, it never points takes it to the other page. Any others suggestions?

<?php

if (empty($_POST))
{
echo "In If Statement";
header("Location: piksheet.htm");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Pick Submission</title>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Redirect

Post by Celauran »

No error messages? Is error reporting on? I'd guess there's a "headers already sent" error being generated by not displayed.
mjstehn
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2004 4:25 pm

Re: PHP Redirect

Post by mjstehn »

Stupid mistake on my part. Forgot to take out my test echo statement. Once I took that out it worked. Thanks for all your help.

Mike
Post Reply