Using a variable in a header...

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

oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Using a variable in a header...

Post by oscardog »

I have the following code, i have honestly tried everything... just putting $page , putting speech marks around it.. putting ' around it.. putting the full URL.. not including Location, including Location.. just putting ' around the $page part... EVERYTHING. Nothing works, if alternatively you have another way of doing this, and its not terribly complex, feel free to suggest.

Code: Select all

<?php
 
$type = $_POST['type'];
$mode = $_POST['mode'];
$month = $_POST['month'];
 
if($type == "Planner" && $mode == "View") {
$page = "Location:view_planner.php?month=$month";
}
 
if($type == "Planner" && $mode == "Edit") {
$page = "Location:edit_planner.php?month=$month";
}
 
if($type == "Diary" && $mode == "View") {
$page = "Location:view_diary.php?month=$month";
}
 
if($type == "Diary" && $mode == "Edit") {
$page = "edit_diary.php?month=$month";
}
 
header("Location: http://www.ashpea1.exofire.net/$page");
 
?>
 
Thanks!
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Using a variable in a header...

Post by papa »

Have you checked that your post vars are set?
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Using a variable in a header...

Post by mmj »

You're having a problem sending get variables when redirecting with the location header?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

Hmm well im pretty sure the post VARS are set, but i'll make sure. But yeh pretty sure, and basically it will take a month, from a form(aswell as type(planner/diary) and mode(edit/view). It will then redirect to the appropriate page, passing on the month taken from the form onto the page it redirects to.

ATM when i use the code pasted below it either puts in the URL ashpea1.exofire.net/Diary/$page or put in the URL with LOCATION before it... Or it will just be a white page, or wierdly occassionally will just remain on the page that the form is on :S
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Using a variable in a header...

Post by papa »

Do:

echo $type = $_POST['type'];
echo $mode = $_POST['mode'];
echo $month = $_POST['month'];

And maybe:

Code: Select all

 
header("Location: http://www.ashpea1.exofire.net/".$page);
But first make sure you have the vars set.

I would probably do a switch statement and have a default url to fall back on.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Using a variable in a header...

Post by novice4eva »

is this some new cool code i dunno abt?? What is that "location:" string doing there??

Code: Select all

 
$page = "Location:view_planner.php?month=$month";
//SHOULDN'T THIS BE JUST
$page = "view_planner.php?month=".$month;
 
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Using a variable in a header...

Post by mmj »

novice4eva wrote:is this some new cool code i dunno abt?? What is that "location:" string doing there??

Code: Select all

 
$page = "Location:view_planner.php?month=$month";
//SHOULDN'T THIS BE JUST
$page = "view_planner.php?month=".$month;
 
:P, Location: Location
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

Right i did the echo thing, all the variables are set correctly, using the header line you suggested Papa, it put in the URL bar:

http://www.ashpea1.exofire.net/Location ... th=January

So close, but no cigar... Its mostly right, just i dont want the location thing in there...

Removing location doesn't work it jsut leaves the page blank... Replacing the speech marks with ' doesnt work.. Stumped :( I checked PHP.net its no help..
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Using a variable in a header...

Post by novice4eva »

oscardog wrote:Right i did the echo thing, all the variables are set correctly, using the header line you suggested Papa, it put in the URL bar:

http://www.ashpea1.exofire.net/Location ... th=January

So close, but no cigar... Its mostly right, just i dont want the location thing in there...

Removing location doesn't work it jsut leaves the page blank... Replacing the speech marks with ' doesnt work.. Stumped :( I checked PHP.net its no help..
all i get is 404 Error with some advertisement in it : with/without Location in the URL!!
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

OMG im such a retard! I forgot i had a a /Diary/PAGE WOW im retarded...

But it still leaves the 'Location' part in the URL, so...
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Using a variable in a header...

Post by papa »

Can you show us your updated code?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

Code: Select all

<?php
 
$type = $_POST['type'];
$mode = $_POST['mode'];
$month = $_POST['month'];
 
if($type == "Planner" && $mode == "View") {
$page = "Location:view_planner.php?month=$month";
}
 
if($type == "Planner" && $mode == "Edit") {
$page = "Location:edit_planner.php?month=$month";
}
 
if($type == "Diary" && $mode == "View") {
$page = "Location:view_diary.php?month=$month";
}
 
if($type == "Diary" && $mode == "Edit") {
$page = "edit_diary.php?month=$month";
}
 
header("Location: http://www.ashpea1.exofire.net/Diary/$page");
 
?>
 
So i added in the missing directory, so the page now exists but ony when i manually remove the Location part from the URL...

13:53 GMT - Web Server is currently down, so dont try and access the website cos it will fail...
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Using a variable in a header...

Post by papa »

As novice4eva said earlier, no location: in the $page variable.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

This is my new updated code, still not working... This time it just leaves a blank page? :S

Code: Select all

<?php
 
$type = $_POST['type'];
$mode = $_POST['mode'];
$year = $_POST['year'];
$month = $_POST['month'];
 
if($type == "Planner" && $mode == "View") {
$page = "view_planner.php?month=$month&year=$year";
}
 
if($type == "Planner" && $mode == "Edit") {
$page = "edit_planner.php?month=$month&year=$year";
}
 
if($type == "Diary" && $mode == "View") {
$page = "view_diary.php?month=$month&year=$year";
}
 
if($type == "Diary" && $mode == "Edit") {
$page = "edit_diary.php?month=$month&year=$year";
}
 
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/$page");
exit;
 
?>
I did add in an extra variable, the year, and added that into the $page variable. I removed "Location" from the header statement. Thanks :)

Edit: Updated with new code, so it works when i move from test server to proper server... Still no luck, although wierdly when i submit the data it just loads the page the form is on... http://www.ashpea1.exofire.net/Diary/diarycentral.php - if you wnt to test it you have to make an account... And if i put echo statements in, to test the variables, they are all fine 100% and it loads diaryprocess.php which is really, really wierd..

And after checking php.net im 100% sure in need the "Location: " part in the header statement...
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Using a variable in a header...

Post by oscardog »

Anyone? :(
Post Reply