Page 1 of 1

Posting A New Header to Redirect Page

Posted: Fri Oct 01, 2010 2:05 am
by jasonv
Hello,

I am new to some of the functions of the PHP header(). I have used it to redirect pages in the past, but currently I am attempting to develop a page that can display a message from a submit button but that basically redirects the index page of a subfolder to a different index page in a different subfolder to relay information that a current business is closed due to inclimate weather.

So basically the admin area of the site would contain two submit buttons, one that says the weather is good which means that it is the normal index page and one that says the weather is bad and they are closed till further notice. Both submit buttons would have a POST function attached, but I do not understand how to get the file I am attempting to edit to open and then to write in the new header location.

Here is some of my code -

Code: Select all

<div style="width:220px; height:50px;">
<form method="POST" action="../../weather.php">

<input type="submit" value="Good Weather" name="weather_good" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Bad Weather" name="weather_bad" />

</form>
</div>
And here is the weather.php

Code: Select all

<?php

if(isset($_POST['weather_good'])){
    $myFile = "../myfile.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
	$stringData = echo "<?php header ('Location:the_location/page_redirect.php'); ?>";
    fwrite($fh, $stringData);
    fclose($fh);
	echo "Page Updated";
   }

else if(isset($_POST['weather_bad'])){
    $myFile = "../myfile.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
	$link =  "header('Location:the_location/page_redirect.php');" ;
	$stringData = $link;
    fwrite($fh, $stringData);
    fclose($fh);
	echo "Page Updated";
	}
?>
Thanks for any and all help!

Re: Posting A New Header to Redirect Page

Posted: Fri Oct 01, 2010 3:39 am
by requinix
Overcomplicating it.

If there's good weather then nothing happens. If there is bad whether then the store is closed. So really, all you need to do is check whether there's bad weather... Heh.
Instead of putting the location in a file, just use the mere existence of the file: if it exists then the store is closed and if not then the store is open.

But first, that form can be simplified. Since the person can only click one button, you can use the same name for both of them:

Code: Select all

<div style="width:220px; height:50px;">
<form method="POST" action="../../weather.php">

<input type="submit" value="Good Weather" name="weather" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Bad Weather" name="weather" />

</form>
</div>
As for the PHP,
- If it's good weather and the signal file exists, delete it
- If it's bad weather and the file does not exist, create it

Code: Select all

<?php

define("SIGNAL_FILE", "../closed");

if (isset($_POST["weather"])) {
    if ($_POST["weather"] == "Good Weather" && file_exists(SIGNAL_FILE)) {
        unlink(SIGNAL_FILE);
    } else if ($_POST["weather"] == "Bad Weather" && !file_exists(SIGNAL_FILE)) {
        touch(SIGNAL_FILE);
    }
}
For redirection, instead of including a file you'll simply check whether that signal file exists.

Code: Select all

<?php

define("SIGNAL_FILE", "../closed"); // should really be put in one file and then include()d here

// ...

if (file_exists(SIGNAL_FILE)) {
    header("Location: /path/to/wherever.html");
    exit;
}

You could actually get a little smarter and turn this into a general-purpose solution. Say there are vacation times when the store is closed as well.
Taking a step back, put a location in that signal file: the location of the "sorry, we're closed" message. The weather script puts one path in there (to the "sorry, we're closed due to inclement weather" page) and the vacation script puts another path (to the "sorry, we're on vacation so the store is closed" page). Then the redirect stuff (a) checks for the file, and if it exists (b) redirects according to whatever URL was in there.

Re: Posting A New Header to Redirect Page

Posted: Sun Oct 03, 2010 12:34 am
by jasonv
Thanks so much for the help. Could I ask for some clarification? Is the SIGNAL_FILE a file I create or is that a function within PHP? I haven't seen that before.

Thank You!

Re: Posting A New Header to Redirect Page

Posted: Sun Oct 03, 2010 1:57 am
by jasonv
I also am not sure if I explained what I am attempting to do correctly.

So I have an index page in the home directory that has a current header location. I am attempting to post a new header location to that index file to point to a message page. Does this apply? I don't think I am doing it right. All it does when I click the bad weather button it just redirects my current page. Sorry if I didn't explain this right the first time.

Re: Posting A New Header to Redirect Page

Posted: Sun Oct 03, 2010 4:51 am
by requinix
What you originally tried to do was put PHP code into a file. More generally, what you wanted was the redirect URL in there.
The first thing I suggested was not worrying about a URL: if the file is there at all then it's bad weather so redirect to the bad weather page, and if not then it's normal weather so redirect to the normal (open) page.

The second thing I said was more along the lines of what you first wanted. If you stick a URL into that file, then: if the file is there then grab the URL from it and redirect there, and if not then it's business as usual so redirect to the business as usual (open) page.

Now,
1.

Code: Select all

define("SIGNAL_FILE", "../closed");
SIGNAL_FILE is a constant defined in the PHP. Your script gives it the value. I picked "closed" in the parent directory.

2. For
- Good weather: delete the file if it exists
- Bad weather: write (perhaps using file_put_contents) the "closed" URL to the file. That could be something like

Code: Select all

/bad_weather.html
What's your code now?

Re: Posting A New Header to Redirect Page

Posted: Sun Oct 03, 2010 10:12 am
by jasonv
Okay, I am beginning to understand.

Here is what I had initially code wise -

Code: Select all

<?php

if(isset($_POST['weather_good'])){
    $myFile = "../myfile.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
        $stringData = echo "<?php header ('Location:the_location/page_redirect.php'); ?>";
    fwrite($fh, $stringData);
    fclose($fh);
        echo "Page Updated";
   }

else if(isset($_POST['weather_bad'])){
    $myFile = "../myfile.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
        $link =  "header('Location:the_location/page_redirect.php');" ;
        $stringData = $link;
    fwrite($fh, $stringData);
    fclose($fh);
        echo "Page Updated";
        }
?>
So, yes I am trying to write to the main page of the site a new header location in order to redirect prospective attendees.

Here is the website - http://www.harvestfarm.net

When you arrive at this page there is already a redirect in place to send you to the correct page of the site in relation to the company's seasonal operations.
Currently, this is in the index.php page of the site -

Code: Select all

<?php header('Location:fall_festival/index.php'); ?>
From the admin area where the buttons are located I am trying to overwrite the current header location if there is bad weather to go to

Code: Select all

<?php header('Location:fall_festival/weather/index.php'); ?>
and then when the weather changes back to replace with the original header so it's business as usual.

I think that's why I started with two buttons initially because I wasn't sure how to return the page to the current state. And the reason I was attempting an echo at the end of the code at the top was to allow the admins to see that the page had been updated and then redirect them back to their page, but I don't think that was implemented yet.

Re: Posting A New Header to Redirect Page

Posted: Thu Oct 07, 2010 2:04 pm
by jasonv
Does this look like a feasible concept or is it kind of pointless to proceed in this fashion?