Jump to content

[SOLVED] Parse error: syntax error, unexpected T_STRING


beastilio

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

 

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.