Jump to content

placing browser sniff php on index HTML


IanS

Recommended Posts

Total php noob.Have a simple php useragent sniffer needed to redirect mobile users, etc...Where is best to place the script within the main HTML index file? Top of page? Before head, after it, in the body? Do bots read this script or ignore it?

Link to comment
Share on other sites

Hmm, that's where I put it, just under the doctype.

 

Is there some reason why something simple like this would not work? --

 

<?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match('/iPhone|Android|Blackberry/i', $agent)){ header("Location: mobile.html"); exit; } ?>

 

Bearing in mind I have no idea what I'm doing with PHP code!

(There is a more complicated version of this, but even this cut-down one isn't working.)

 

Could use JavaScript, but I'd prefer this to work.

 

many thanks.

 

btw, php does run on my server. I have forums working just fine.

 

Link to comment
Share on other sites

It doesn't work because you put it after the doctype. It goes before anything else in the file. If you're redirecting then you can't send any output. Output is anything outside of the PHP tags, including blank spaces or the doctype.

Link to comment
Share on other sites

Turns out this code didn't work after all. Beats me why.

I'll have to use javascript...

 

(But then the viewport meta isn't working on iPhone either.

Not having much luck with this mobile site all round!)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...