Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Can you put javascript at the beginning of the index.php so that it is the first code that loads on a webpage?

Ive tried, however the website won't load at all. I need to use this code to find out if the user is clicking a link to full an article on the full site from the mobile site.

C#
if ( document.referrer.search('http://oursite.com')== 0 )
{
   include "index2.php";
}
else
{
   include "mobiledetection.php";
   /* plus the other code already in the index file*/
}


The index2.php being the code that was in the index file before.
Ive attempted to use the document.referrer function without javascript, and try it in an external js but either works
Posted

1 solution

It looks like you're mixing up Javascript and PHP. document.referrer is Javascript and include is PHP.

Since Javascript happens on the client, you would need to either display a page containing your Javascript code and redirect to another to get the effect you want, or do it all in PHP where you can use the include command:
PHP
<?php
// yes, this is how HTTP_REFERER is spelled
if(isset($_SERVER['HTTP_REFERER']) && 
  strpos($_SERVER['HTTP_REFERER'],'http://oursite.com') === 0) {
  include "index2.php";
} else {
  include "mobiledetection.php";
}
?>


You should also note that the referrer is not reliable - the browser does not have to send it, so you should decide what to do if you don't know where the visitor has come from.

For testing, you can disable sending referrer details using the Web Developer FireFox add-on.
 
Share this answer
 
Comments
RaviRanjanKr 29-Dec-11 18:09pm    
5+
samcon34 31-Dec-11 7:33am    
how to you allow for multiple searches? so 'http://oursite.com', 'http://yoursite.com' etc.
Ive tried many combinations using && || , etc but it seems to just stop it from working all together.
Thanks!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900