Jump to content

Easy help with prewritten mobiledetect script


superDR01D

Recommended Posts

I need a tiny shove in the right direction on how to implement this prewritten mobile detection script.  It comes up #1 on Google but it has no instructions.  Could anybody help me with this?

 

http://mobiledetect.net/

 

Q1) I believe this code needs to be placed at the top of every HTML file on my site.  But where in the HTML file do I place it?  When I put it at the very top of my index.htm it doesn't run.  The code prints out like plain text at the top of the webpage.

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;

// change the mobile url here (between the quotes)
$mobileurl = "http://m.mywebsite.com";

$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

if( $detect->isMobile()){

header("Location: " . $mobileurl,true, 301); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
}
?>

Q2) Mobile_Detect.php  I believe I need to upload this file to my root directory.  That's easy enough but does it need special permissions or something else?  Right now permissions are set to 0644 on this file.

 

Q3) Do I need to update my .htaccess file which is blank?  I'm hosting with GoDaddy so I believe it's on an Apache server.

 

Q4) Is there any other obvious thing that I'm missing?

 

Thanks a ton in advance!!!

 

 

Link to comment
Share on other sites

 

I believe this code needs to be placed at the top of every HTML file on my site.

 

Nope :) , its a PHP class that requires to be processed server side using PHP.

plain html files are generally passed as is from your server (website) to the client(user with the browser) without passing them through PHP.

 

So to use it: you need to use a PHP file (say inde.php), that has the above code

 

The following line :

 

require_once 'Mobile_Detect.php';

Tells PHP where to look for Mobile_Detect.php . In this case its the same directory as the inde.php file we created earlier.

 

btw you can rename the inde.html file to inde.php , but that might break things depending on how the rest of your site is...

 

Lastly I think the apache server can be configured to process html files as PHP, but I am pretty sure thats (a) not a good idea (b) not possible on shared hosting...

Link to comment
Share on other sites

Awesome thank you so much!!!!

 

Ok so...

 

1) Put that php script in a file called inde.php and upload it to my root directory.

2) Upload mobile_detect.php to my root directory.

 

My only question is what triggers those 2 PHP files to be served up to people who visit my site?  Do I have to call them somehow in my index.htm file?  Or maybe set something up on the server so that inde.php is called as soon as someone visits my site?

 

BTW my site structure is very basic.

 

index.htm and the rest of my htm files are in the root directory.  I created a m (mobile) sub directory for my mobile website.  m.mywebsite.com

 

Thanks a ton!!!

Edited by superDR01D
Link to comment
Share on other sites

 

Or maybe set something up on the server so that inde.php is called as soon as someone visits my site?

Yup

 

Basically we are replacing index.html with index.php (index.php should be called automatically). HTML files cannot normally call php files.

 

So rename index.html to index.php and add the code there.

 

Its like this:

Apache recieves request for site.com >

it looks for specified files (generally index.html / index.php etc)

If index.html it sends as is.

index.php it processes it through PHP , which creates html text and sends it to apache , which sends it to the browser.

 

 

HOWEVER you will also have to rename all instances of index.html in your other files to index.php, otherwise dead links...

btw if you are interested to go deeper into PHP I strongly recommend a book/video tutorial series to grasp the basics.

Link to comment
Share on other sites

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.