...

View Full Version : Using PHP header as a variable



Psirus
04-05-2008, 06:26 PM
Is it possible?

I want to check to see a users browser. If it is FF, direct them somewhere.


<?php
$redirect ='header( \'Location: http://localhost/portfolio/blog\' ) ';
?>


<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
echo $redirect
?>


I know you can't output any text before a header, and I havn't. What comes up in my browser is


header( 'Location: http://localhost/portfolio/blog' )

flynch01
04-05-2008, 06:53 PM
Echo is used to output text. You want to call the header as a function. Like so:


<?php
$redirect = 'Location: http://localhost/portfolio/blog';
?>


<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
header($redirect);
?>

However this is untidy code you could shorten the whole thing to be inside only one <?php?> tag and without using a variable:



<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
header('Location: http://localhost/portfolio/blog');
?>

Psirus
04-05-2008, 07:01 PM
I get you on the echo, thanks :)

I get this error when using the bottom code you posted:


Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\portfolio\index.php on line 3

flynch01
04-05-2008, 07:56 PM
Sorry went for dinner. Yeah I only had a quick glance before so I didn't spot it. You opened two parenthesis and only closed one:

This:


<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
header('Location: http://localhost/portfolio/blog');
?>

Becomes:


<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
header('Location: http://localhost/portfolio/blog');
?>

jaap
04-05-2008, 08:22 PM
.. and don't forget: you cannot output anything before the header. So be careful that you don't type any spaces or anything before the starting "<?php".

Second, to be sure, always use an
exit(0); after using the header. This prevents the script from being parsed if the header does not do its job.

good luck!

Psirus
04-05-2008, 08:46 PM
Thanks guys mucho love!

Basically I just made a portfolio page, which does not work what-so-ever in IE. So if they have firefox they get redirected, if not then they get told off.

PappaJohn
04-05-2008, 09:24 PM
So, instead of fixing your page to work in IE, you took additional steps to alienate the largest percentage of web users and, in the same stroke, effectively killed any favorable SE treatment of your page.

Good luck with it, but why would you bother?

Psirus
04-06-2008, 12:02 AM
So, instead of fixing your page to work in IE, you took additional steps to alienate the largest percentage of web users and, in the same stroke, effectively killed any favorable SE treatment of your page.

Good luck with it, but why would you bother?


I tried.
It has too many issues.

Basically I created a site blah blah, then I put a section /blog/ in there and installed wordpress, edited the css etc. so it works fine in FF, and every other browser.

I figured out some things, but there are too many to deal with.

I know it is bad... I just don't know what else to do.

What is SE treatment?

PappaJohn
04-06-2008, 12:44 AM
True, IE causes seemingly no end of problems, but ignoring IE doesn't make the problems go away. IE is firmly entrenched and is not going to disappear (at least not anytime soon).

I guess I just don't get the reasoning behind going through the trouble to put up a website, especially one with a blog which you'll, presumably, be updating only to have it inaccessible to the majority of users.


I know it is bad... I just don't know what else to do.
If you're serious about fixing it, post in the HTML forum. There are plenty of helpful, knowledgeable folks that hang around.


What is SE treatment?
Search Engines. Since the search engines don't browse with Firefox, all they'll ever index is your rant about not using IE - not very conducive to attracting visitors.

Psirus
04-06-2008, 12:49 AM
By all means I don't intend this to be a final solution to my problem.

Thank you very much for your advice. I guess I will hop on over to the HTML and CSS guys and ask them for a hand. :)

PappaJohn
04-06-2008, 12:50 AM
You're welcome. I think that's a better approach.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum