james909 Posted March 30, 2013 Share Posted March 30, 2013 (edited) when my page is visited i am trying to get the page to reload with a random number variable added to the URL, so if http://mysite.com/example/ is visited, it reloads the page as either http://mysite.com/example/?outpost=1 or http://mysite.com/example/?outpost=2 this is the .PHP file http://mysite.com/example/index.php, i have commented to the side of the PHP code with what i am trying to achieve <!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" xmlns:fb="http://ogp.me/ns/fb#" xml:lang="it"> <head> <?php $base_url = "http://mysite.com/example"; //this is actually the correct base link, to the url $randompicstart=rand(1,2); //gets a random number either 1 or 2 if (empty($_GET['v'])) //if variable v is empty then { $v=$randompicstart; //assign the random number as variable v } if(!isset($_GET['outpost'])) //if the variable outpost is not in the url then { header("Location: $base_url/?outpost=$v"); //set the url with the outpost variable added as the value from v variable exit; //exit this url and reload new url which will have the outpost variable added } ?> </head> <body> <p>This is my HTML code for the page</p> </body> </html> it is just returning a blank page and keeping the url the same, with no outpost variable added to the URL what is the problem with my code? thank you for reading my problem, james Edited March 30, 2013 by james909 Quote Link to comment https://forums.phpfreaks.com/topic/276329-trying-to-get-a-variable-added-to-a-url-and-reload-page/ Share on other sites More sharing options...
PaulRyan Posted March 30, 2013 Share Posted March 30, 2013 You cannot output any content before the header redirect, you would know this if you turned on error reporting. See here: Error Reporting Once you have error reporting switched on, it will help with future development. Put all PHP code above the <doctype element, and it should work nicely. Quote Link to comment https://forums.phpfreaks.com/topic/276329-trying-to-get-a-variable-added-to-a-url-and-reload-page/#findComment-1421975 Share on other sites More sharing options...
james909 Posted March 30, 2013 Author Share Posted March 30, 2013 thank you very much paul! i put the header redirect before any HTML and it is working as it should, thanks Quote Link to comment https://forums.phpfreaks.com/topic/276329-trying-to-get-a-variable-added-to-a-url-and-reload-page/#findComment-1421977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.