Header Location Issue

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

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Header Location Issue

Post by s.dot »

I have this code... in which the header: location line is not working. It's the last line of the code, all of the rest of the code may be irrelevant, but I posted it just in case.

Code: Select all

if($action == "postreply"){
if(!isset($_COOKIEї'username'])) { echo "You must be logged in to post in the forums."; require 'forumsfooter.php'; die(); }
if(!entry) { echo "You cannot submit an empty reply"; require 'forumsfooter.php'; die(); }
$time2 = time();
$forumsql = "SELECT id, topicname, forumid FROM forumtopicmain WHERE id = '$threadid'";
$forumquery = mysql_query($forumsql);
$forumarray = mysql_fetch_array($forumquery);
$forumnamesql = "SELECT id FROM forumtopics WHERE id = '".$forumarrayї'forumid']."'";
$forumnamequery = mysql_query($forumnamesql);
$forumnamearray = mysql_fetch_array($forumnamequery);
$forumaid = $forumnamearrayї'id'];
$date = date("M d");
$date2 = date("g:i A");
$date3 = "$date at $date2";
$smilies = array("::arrow::", "::badgrin::", "::biggrin::", "::confused::", "::cool::", "::cry::", "::doubt::", "::evil::", "::exclaim::", "::idea::", "::lol::", "::mad::", "::neutral::", "::question::", "::razz::", "::redface::", "::rolleyes::", "::sad::", "::shock::", "::smile::", "::surprised::", "::wink::");
$smiliesimg = array("<img src=smilies/arrow.gif>", "<img src=smilies/badgrin.gif>", "<img src=smilies/biggrin.gif>", "<img src=smilies/confused.gif>", "<img src=smilies/cool.gif>", "<img src=smilies/cry.gif>", "<img src=smilies/doubt.gif>", "<img src=smilies/evil.gif>", "<img src=smilies/exclaim.gif>", "<img src=smilies/idea.gif>", "<img src=smilies/lol.gif>", "<img src=smilies/mad.gif>", "<img src=smilies/neutral.gif>", "<img src=smilies/question.gif>", "<img src=smilies/razz.gif>", "<img src=smilies/redface.gif>", "<img src=smilies/rolleyes.gif>", "<img src=smilies/sad.gif>", "<img src=smilies/shock.gif>", "<img src=smilies/smile.gif>", "<img src=smilies/surprised.gif>", "<img src=smilies/wink.gif>");

		// Linking function //

function make_clickable($text) 
&#123; 

   $ret = ' ' . $text; 
   $ret = preg_replace("#(^|&#1111;\n ])(&#1111;\w]+?://&#1111;^ "\n\r\t<]*)#is", "\\1<a href="\\2" target="_blank">\\2</a>", $ret); 
   $ret = preg_replace("#(^|&#1111;\n ])((www|ftp)\.&#1111;^ "\t\n\r<]*)#is", "\\1<a href="http://\\2" target="_blank">\\2</a>", $ret); 
   $ret = preg_replace("#(^|&#1111;\n ])(&#1111;a-z0-9&\-_.]+?)@(&#1111;\w\-]+\.(&#1111;\w\-\.]+\.)*&#1111;\w]+)#i", "\\1<a href="mailto:\\2@\\3">\\2@\\3</a>", $ret); 
   $ret = substr($ret, 1); 
   return($ret); 
&#125;

$entry2 = str_replace($smilies, $smiliesimg, $entry);
$entry3 = str_replace("&#1111;IMG]", "<img src=", $entry2);
$entry4 = str_replace("&#1111;/IMG]", ">", $entry3);
$entry5 = str_replace("&#1111;QUOTE]", "<blockquote class=code>", $entry4);
$entry6 = str_replace("&#1111;/QUOTE]", "</blockquote>", $entry5);
$entry7 = mysql_real_escape_string(nl2br(make_clickable(strip_tags($entry6, '<img>,<blockquote>'))));

$insertreplysql = "INSERT INTO forumentries (entry, time, topicid, forumid, istopic, author, time2) VALUES ('$entry7', '$date3', '$threadid', '$forumaid', 'n', '".$_COOKIE&#1111;'username']."', '$time2')";
mysql_query($insertreplysql);
$forumsql = "SELECT topicname, forumid FROM forumtopicmain WHERE id = '$threadid'";
$forumquery = mysql_query($forumsql);
$forumarray = mysql_fetch_array($forumquery);
$inserttimereply = "UPDATE `forumtopicmain` SET `lastreply` = '$time2' WHERE id = '$threadid'";
mysql_query($inserttimereply) or die(mysql_error());
header("Location: showthread.php?threadid=$threadid&page=$page");
The header variables are set in the URL. And PHP doesn't throw any errors out on me, I just end up with a blank page.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

showthread.php?threadid=$threadid&page=$page
In showthread.php

Code: Select all

echo $_GET&#1111;'threadid'];
echo '<br>';
echo $_GET&#1111;'page'];
What is it showing ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it appears $threadid and $page are not set anywhere.. are register_globals on?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

They are passed from the form action and are in the URL of the page... for example http://www.domain.com/showthread.php?threadid=87&page=3

Register globals are on.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Can you add this to the beginning of the script right after <?php

Code: Select all

error_reporting(E_ALL);
With that in place... When you run your script what do you see? Any kind of message from the browser? A completely blank screen?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

No errors, just a blank page
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

What happens is you add an echo after the header statement? Do you see that text?

Code: Select all

echo "Made it here";
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Anything I add after the header("Location: page"); still works, I currently have a link so the users can click on to go to their forum post.

But if the header location was working correctly, it would appear seamless to the user.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

When there is a blank page, what do you see if you do a view source? If there is anything at all in the view source, put it here so I can see it.

Are you just trying to redirect the user after a post? If so, have you conisidered using META REFRESH instead of location?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Okay, I figured it out. I head it showing a body background color, which was preventing the header from being executed

but why didn't it throw out an error at me?
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

It was up to the browser to honor that request. PHP was working fine, the browser ignored it because it had already received content. Glad to hear that it works.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

thanks for your help :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

will the PHP header location always work, or should I provide a backup link just in case?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a properly compliant browser to http standards will always work.. however, relative URL's in a Location header are only supported in newer browsers as this is a relatively recent addition to the standards. For safety, always use a fully qualified URL.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

You could try displaying the link after you do the location as a fallback. I haven't tried it but it is a good theory. :)

This site displays an intermediate page and does a meta refresh instead of location and it works pretty well.
Post Reply