maxmadx Posted January 3, 2008 Share Posted January 3, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/ Share on other sites More sharing options...
teng84 Posted January 3, 2008 Share Posted January 3, 2008 $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 ' Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-428968 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-428974 Share on other sites More sharing options...
teng84 Posted January 3, 2008 Share Posted January 3, 2008 instead of answering that better if you find the error by your self... heres waht to do echo $referer and see it it matches your condition Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-428977 Share on other sites More sharing options...
stuffradio Posted January 3, 2008 Share Posted January 3, 2008 You could also use print_r($referer); That tells you all the information you need to know Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-428981 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-428989 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 HMM this is tough for me, can anyone else help me? Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429022 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429025 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 ??? it only says array for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429042 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 the prev code did show google when it was google that was referring, so that part seems to work. But it just isnt directing me to the other page when its a url typed into the browser. Very annoyinG!! hehe Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429043 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 try changing 'echo $referer;' to: echo '<pre>'; print_r($referer); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429052 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 hi, its creating error: unexpected T_ECHO with echo, and with echo ' it doesnt allow the " ' " Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429055 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 Forgot code tags. d'oh. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429061 Share on other sites More sharing options...
maxmadx Posted January 3, 2008 Author Share Posted January 3, 2008 whats code tags? Dont happen to know the complete code for what i am looking for do you? ~I am not a programmer and ocne ive done this wont likely use php again. Thanks for your help man Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429064 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-429071 Share on other sites More sharing options...
maxmadx Posted January 4, 2008 Author Share Posted January 4, 2008 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- *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 Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430109 Share on other sites More sharing options...
Ken2k7 Posted January 4, 2008 Share Posted January 4, 2008 What are you trying to do with the $referer variable? Are you trying to get the URL in the address bar? Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430111 Share on other sites More sharing options...
maxmadx Posted January 4, 2008 Author Share Posted January 4, 2008 Hi, I run adwords, and want to so that when someone comes to my site from google it will go to a specific page, and all other ways, like putting the link directly into the address bar, go to another. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430114 Share on other sites More sharing options...
Ken2k7 Posted January 4, 2008 Share Posted January 4, 2008 So wouldn't this be enough: <?php if ($_SERVER['HTTP_REFERER'] == "http://www.whatever.com") header("Location: whatever.com"); else header("Location: elsewhere.com"); ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430116 Share on other sites More sharing options...
maxmadx Posted January 4, 2008 Author Share Posted January 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430120 Share on other sites More sharing options...
Ken2k7 Posted January 4, 2008 Share Posted January 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84234-new-to-php-can-you-help-why-this-gives-an-else-statement-error-plz/#findComment-430156 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.