beastilio Posted January 4, 2008 Share Posted January 4, 2008 Ok so I have a simple script that sends a user to a .com site. So redirect.php?SITENAME=google sends them to google.com I get the error mentioned in the topic name with this code: <?php if(isset($_GET['SITENAME'])) { header('Location: http://www.{$_GET['SITENAME']}.com'); } ?> Thanks in advance, Beastilio Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/ Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Share Posted January 4, 2008 Should be <?php if(isset($_GET['SITENAME'])) { header('Location: http://www.'.$_GET['SITENAME'].'.com'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429914 Share on other sites More sharing options...
StormTheGates Posted January 4, 2008 Share Posted January 4, 2008 The problem is your dual quote thingies Remove them from this: {$_GET['SITENAME']} Turn it into this: {$_GET[sITENAME]} Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429915 Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 don't put your "Location...." in single quotes, use double... <?php header("Location: http://www.{$_GET['SITENAME']}.com"); ?> Look at the difference in how the code get's syntax hilighted...the "SITENAME" is intrepreted as a misplaced string in your example because you are using single quotes around the whole string. Turn it into this: {$_GET[sITENAME]} Doing so causes an extra operation for the processor as it checks to see if there is a constant named SITENAME before trying with the string "SITENAME". It's better practice to use quotes around your array keys. My preferred method is the same as phpsensei. Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429916 Share on other sites More sharing options...
beastilio Posted January 4, 2008 Author Share Posted January 4, 2008 Should be <?php if(isset($_GET['SITENAME'])) { header('Location: http://www.'.$_GET['SITENAME'].'.com'); } ?> Thanks, but I get "syntax error, unexpected '{' " Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429921 Share on other sites More sharing options...
teng84 Posted January 4, 2008 Share Posted January 4, 2008 The problem is your dual quote thingies Remove them from this: {$_GET['SITENAME']} Turn it into this: {$_GET[sITENAME]} bad practice better to replace the single quote with double qoute if(isset($_GET['SITENAME'])) { header("Location: http://www.{$_GET['SITENAME']}.com"); } Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429922 Share on other sites More sharing options...
StormTheGates Posted January 4, 2008 Share Posted January 4, 2008 Turn it into this: {$_GET[sITENAME]} Doing so causes an extra operation for the processor as it checks to see if there is a constant named SITENAME before trying with the string "SITENAME". It's better practice to use quotes around your array keys. My preferred method is the same as phpsensei. Well Ill be damned, learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429923 Share on other sites More sharing options...
beastilio Posted January 4, 2008 Author Share Posted January 4, 2008 don't put your "Location...." in single quotes, use double... <?php header("Location: http://www.{$_GET['SITENAME']}.com"); ?> Look at the difference in how the code get's syntax hilighted...the "SITENAME" is intrepreted as a misplaced string in your example because you are using single quotes around the whole string. Turn it into this: {$_GET[sITENAME]} Doing so causes an extra operation for the processor as it checks to see if there is a constant named SITENAME before trying with the string "SITENAME". It's better practice to use quotes around your array keys. My preferred method is the same as phpsensei. I get Warning: Cannot modify header information - headers already sent by (output started at /redirect.php:11) in /redirect.php on line 13 with that code Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429924 Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 It tells you the error: headers already sent by (output started at /redirect.php:11) Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429928 Share on other sites More sharing options...
StormTheGates Posted January 4, 2008 Share Posted January 4, 2008 You could always echo a meta refresh Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429930 Share on other sites More sharing options...
awpti Posted January 4, 2008 Share Posted January 4, 2008 You can have NOTHING output before the header() redirect. Not even whitespace. <?php header('Location: ...'); ?> .. fails. <?php header('Location: ...'); ?> .. fails. <?php echo ' '; header('Location: ...'); ?> .. fails. Get it? <?php header('Location: ...'); ?> Would work. Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429931 Share on other sites More sharing options...
beastilio Posted January 4, 2008 Author Share Posted January 4, 2008 bad practice better to replace the single quote with double qoute if(isset($_GET['SITENAME'])) { header("Location: http://www.{$_GET['SITENAME']}.com"); } Ok I did that, that is what my code looks like. But now I get: < Warning: Cannot modify header information - headers already sent by (output started at /home/par10000/public_html/script/redirect.php:11) in /home/par10000/public_html/script/redirect.php on line 12 ??? -Beastilio Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429932 Share on other sites More sharing options...
beastilio Posted January 4, 2008 Author Share Posted January 4, 2008 Here, Ill post the link so people know what I am talking about. Also thanks so far to everyone that's helped! http://tinyurl.com/2yaau8 Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429935 Share on other sites More sharing options...
beastilio Posted January 4, 2008 Author Share Posted January 4, 2008 You can have NOTHING output before the header() redirect. Not even whitespace. Haha, YES! Thanks, I had the "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">" before the php. Thanks everyone!! -Beastilio Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429937 Share on other sites More sharing options...
teng84 Posted January 4, 2008 Share Posted January 4, 2008 if(isset($_GET['SITENAME'])) { header("Location: http://www.{$_GET['SITENAME']}.com"); exit; } note that will work if you don't output anything at the top OK again the error say you cant echo / output something when using header but you can try ob_clean to avoid that but i don't suggest that Quote Link to comment https://forums.phpfreaks.com/topic/84406-solved-parse-error-syntax-error-unexpected-t_string/#findComment-429938 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.