Jump to content

New to PHP- can you help why this gives an else statement error plz?


Recommended Posts

<?php

// get the page the user came from and store it in the variable $referer

$referer = parse_url(htmlspecialchars(strip_tags($_SERVER['HTTP_REFERER'])));

// if the variable $referer is 

// equal to http://www.google.com then

// redirect them to http://www.camefromgoogle.com

// else direct to http://www.didntcomefromgoogle.com

if ( $referer != "http://www.google.com" ){

  header('Location: http://www.camefromgoogle.com);}

else{

header('Location: http://www.didntcomefromgoogle.com);}

?>

 

 

The error that comes up is:

 

Parse error: syntax error, unexpected T_ELSE in /home/myusername/mysite/index.php on line 10

 

What I want to happen is that if the user comes from google, they will be sent to different page than if from anywhere else. 

 

Can anyone help complete this code?

 

Thank you for anyones help

$referer = parse_url(htmlspecialchars(strip_tags($_SERVER['HTTP_REFERER'])));
if ( $referer != "http://www.google.com" ){
     header('Location: http://www.camefromgoogle.com');}
else{
   header('Location: http://www.didntcomefromgoogle.com);}
?>

 

you forgot the ' 

OH right hehe thanks!

 

However, this code isnt doing what its supposed to, its redirecting me always to camefromgoogle.com despite typing the url directly into browser.  What could be the cause of this?

 

 

Thank you

ok, it is bringing the referer correctly, I wonder if its the fact I am just putting the url into the browser, as this would mean there is no referrer, should I be putting another condition, like if no referrer then do x?

Try this:

 

<?php

$referer = parse_url(htmlspecialchars(strip_tags($_SERVER['HTTP_REFERER'])));

echo $referer; // What's the actual value of $referer?

/*
if ( $referer != "http://www.google.com" )
{
     header('Location: http://www.camefromgoogle.com');
}
else
{
   header('Location: http://www.didntcomefromgoogle.com);
}
*/
?>

 

Debugging isn't difficult, but it isn't easy to do if you're new to PHP. That should give you the output of $referer. See if it is what you expect it to be.

Look up in the post - I gave you some code to place in the file to get the output of $referer

 

I forgot the forum 'BBCode' Code Tags (which stops HTML from parsing).

 

I'll post it again:

 

try changing 'echo $referer;'

 

to...

 

echo '<pre>';
print_r($referer);
echo '</pre>';

 

.. and then give us the output of that.

Ok, when putting the given code i get:

Array

(

    [path] =>

)

 

* when putting address directly into address bar

 

and I get:

 

Array ([scheme] => http [host] => translate.google.com [path] => / translate [query] => or = http% 3A% 2F% 2Fwww.mysite.com% 2Findex.php langpair = en &% 7Cen & hl = en & ie = UTF-8)

 

*when coming from google

 

Looks to be ok, so I tried with full code:

 

<?php

 

$referer = parse_url(htmlspecialchars(strip_tags($_SERVER['HTTP_REFERER'])));

 

 

if ( $referer != "http://www.google.com" )

{

    header('Location: http://www.camefromgoogle.com');

}

else

{

  header('Location: http://www.didntcomefromgoogle.com');

}

 

?>

 

 

* And now nothing happens goes to a standard IE error page with no php type feedback to report.

by typing directly into the address bar

hi

 

gives me an error:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/username/mysite/index.php:2) in /home/maxmadx/victarchandlar/index.php on line 6

 

 

Can I see the entire code or is that it? Because I tested that myself and it worked for me.

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.