How to make a m.mysite.com mobile site in PHP? - php

I am following the link to make wildcard domains. Now m.mysite.com would redirect to mysite.com. But I want it to show a different versions of the site. So that m.mysite.com would be different from mysite.com. My mobile version is put in the htdocs/mobile folder.
So I tried
if (array_shift((explode(".",$_SERVER['HTTP_HOST']))) == 'm'){
include("mobile".$_SERVER['PHP_SELF']);
die();
}
The problem is, I cannot include the Get variables in the include. So if the url is m.mysite.com/read.php?id=1, the part id=1 would be missing.
How should I do it properly?
Note: This question is not about how to redirect different users to different sites according to their devices. But this is about how to set up two sites which shows different versions with a different subdomain.

One way can do this is to download a class that detects what type of device the site is running on (ie. PC, tablet, phone) then load the CSS file appropriately. I have used this class a few times and it seems to work fine.
https://code.google.com/p/php-mobile-detect/
From here you can build up the classes and modify it in whatever way you please.

Solution :
Put two folders in same root directory. First folder is mapped with mysite.com that contain different content and Second one with m.mysite.com with different content.By doing this you can easily access both domains with different content on same web server.

There Is Two Way To Do This Work Simply....
First You Can Detect A Mobile And Redirect Them To A Extra Folder Using This Code
<?php
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable
if (preg_match("/iPhone/", $agent) OR preg_match("/android/", $agent)) { //Detect Mobile Device(You Can Add More Mobile User Agent)
// Send Mobile User To Their Site
header("location: mobile.php"); //ypu can also reirect to a folder or subdomain
}
?>
There Is The Second A Way...You Have To Place This Code In Your Index...
<?php
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable
if (preg_match("/iPhone/", $agent) OR preg_match("/android/", $agent)) { //Detect Mobile Device(You Can Add More Mobile User Agent)
include'mobile.php'; // mobile version
}
else{
include'pc.php'; // dekstop version
}
?>

Related

How can I restrict URL shortcuts on my website?

What I want is rather simple but I cannot figure it out. I want that if someone tries to access any of the webpages directly on my website then he/she must be redirected to the home page of the website.
For Example : Someone tries to access www.domain-name.com/aboutus he gets re directed to www.domain-name.com/
you can check referrer to block any direct access to your website.
include this code in every page of your website.
<?php
$ref = $_SERVER['HTTP_REFERER'];
$url = "http://www.domain-name.com";
// $url is the main url of your website
if($ref==$url){}else{
header("location:$url");
}
?>
But it is completely deepened on browser to send the referrer header,
so it may not work sometimes.
You can use 2 way do it:
In your code, directed www.domain-name.com/aboutus to www.domain-name.com
Config webserver (apache, nginx)

How to serve Mobile / Desktop on same URL wthout m.version nor Media queries?

I want the same URL website.com serving totally different content based on whether mobile or not.
This means, no CSS media queries, and no specific URL m.website.com.
I don't even know if this is possible. But I think I have seen this on this website (not sure).
In other words, imagine a "mobile" folder and a "desktop" folder on the server.
Would it be possible to serve the content of "mobile" folder to the root website.com/ if mobile, and serve desktop if not.
You can do it with PHP and the HTTP User Agent:
function find_mobile_browser()
{
if(preg_match('/(alcatel|android|blackberry|benq|cell|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mobi|motorola|nokia|palm|panasonic|philips|phone|sagem|sharp|smartphone|sony|symbian|t-mobile|up\.browser|up\.link|vodafone|wap|wireless|xda|zte)/i', $_SERVER['HTTP_USER_AGENT']))
{
return true;
}
return false;
}
Call this Function in the beginning of a code and it returns True if your user uses a Mobile User Agent. Ant then you can include the actual site. The if-Block could look like this: (Im not sure about the Include/Requested URI at the Moment)
if($_SERVER['REQUESTED_URI'] == ''){
$request = index.html;
}
else{
$request = $_SERVER['REQUESTED_URI'];
}
if(find_mobile_browsers()){
include('mobile/'.$request);
}
else{
include('desktop/'.$request;
}
Be Aware that the User Agent can be changed or doesn't get send to you but in this Case it would just return the Desktop Version.
For the .htaccess see this site: http://jrgns.net/redirect_request_to_index/

"View Full Site" button and cookie with PHP and jQuery Mobile

I have a website, with a main version and a mobile version. I'm trying to create a "view full site" button that does two things when clicked:
Set a cookie so the site can remember that the button was clicked.
Navigate to the main (not mobile) version of the site.
Here is how I went about doing it:
Create the button.
On click, navigate to a processor page with PHP that sets the cookie, and then redirects to the main version of the site.
Add PHP to the main version of the site that checks if the "view full site" button has been clicked.
Here is what my site is built with:
jQuery Mobile (site is built with this)
PHP mobile detect
jQuery cookie
But it's not working.
On the index of the main version of the site, on the header above the opening HTML tag, I have this:
if(!isset($_COOKIE['dontDoMob'])){
//its not set so check for device type.
include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() ) {
//"its some kind of mobile device so redirect to mobile";
header("Location:http://myforecyte.com/m/");
exit();
}
}
On the index of the mobile site, I added the button, and set on click to set the cookie via jQuery, and then redirect to home page which should continue to load the main version of the site thanks to the cookie.
But it didn't work: it wouldn't even redirect. I wasn't sure if it was jQuery Mobile's fault, so I separated everything. What I did was set the "view full site" link within jQuery mobile to go to a separate processor page (like <a href="processorpage.php" rel="external">), which has this code on it:
setcookie('dontDoMob', 'yes');
header("Location:http://www.myforecyte.com");
But it always goes back to the mobile version. I see the main site's URL for a split second, and then it redirects to the mobile again. As if the cookie wasn't set.
I think it has something to do with setting the cookie on a global scale or something like that.
Note: I checked to make sure that the process of setting the cookie worked fine, with this code:
if(isset($_COOKIE['dontDoMob'])){
echo "its set yo";
} else {
echo "its not set";
}
It works and tells me "its set yo". When I delete all cookies and retest with firebug, it always works accordingly.
I've been trying to do the exact same thing and I finally got it working. I used the same method as you did so I'm not sure why yours doesn't work...
On my mobile site I have a button to view the full site. This button links to www.fullsite.com/setmobile.php
setmobile.php is a simple php file like this:
<?php
setcookie("mobile", "full", time()+3600);
header("Location:http://www.fullsite.com");
?>
It sets the cookie and goes to the main site. The main site has the following php code:
<?php
if (!isset($_COOKIE["mobile"])){
(...detect mobile script)
}
?>
It works for me but it looks like the same method you use so I'm not sure why yours is not working...
You have probably found a solution by now but if not, I hope this can help you :)
Good luck,
Andrew
Cookies have a 'domain' restriction,
e.g. by default, a cookie for http://mobile.example.com is not available to http://www.example.com.
However, it is available to all sub domains of the domain that created the cookie. E.g. http://a.mobile.example.com and http://b.mobile.example.com.
You can specify which domain the cookie should be created for when setting the cookie. Read the PHP manual here:
http://php.net/manual/en/function.setcookie.php
Not sure if this is the full answer for your problem, but since you're mentioning multiple domain names, this may be part of the problem.
Hope this helps

Switch between mobile and full site

All my full site pages are under folder public_html, which contains index.php and folder m which contains all the mobile site pages. index.php is like this:
<?php
if (...mobile client) {
header('Location: m/');
} else {
include 'front.html.php';
}
?>
There is also an index.php in m, simply `include 'front.html'.
This script can detect user's client and direct to full site and mobile site automatically.
But now I want a link on the mobile site to switch to full site. If it is like switch to full site, there will be front.html.php in the address bar, and I don't want this. However, if it is like switch to full site, it will detect again and back to mobile site.
How to deal with that?
You can accomplish this by setting a cookie.
setcookie("forceClient", "full", time()+3600);
Then from that php script, redirect to the home page.
In your index.php, check the cookie:
if($_COOKIE["forceClient"] == "full"){
//logic
}
Make a session variable
like
$_SESSION['viewer_type'] = "Mobile";
or
$_SESSION['viewer_type'] = "Regulare";
and then define a new variable call it base_url and save it in the session also
and do this
if($_SESSION['viewer_type'] == 'Mobile'):
$_SESSION['base_url'] = "http://www.m.site.com/";
else:
$_SESSION['base_url'] = "http://www.site.com/";
endif;
the link now will be
Test
You need to set the session variable each time the view changed from mobile to regular or visa versa

domains redirecting to specific pages

I have a main domain name associated with a WordPress site, and then I have a couple other domain names connected to that site as well.
I want the other two domains names to point/redirect to specific pages on the site rather than the index page, which is the default.
So when domain1.com is typed into the browser, it goes to maindomain.com/domain1page/ (this is how the permalinks are set up). Is this possible?
Add this to index.php and upload it the root of domain1.com
<?php
header("location:http://maindomain.com/domain1page/");
?>
OR if you do not have a Hosting package for domain1.com go to the Domain Manager and in Nameservers you can enter a URL to redirect your domain to there.
It's possible, but you must add each domain manually. Just redirect them to your page.
Sure - there are lots of different ways to do this. Some registrars let you set up redirects at the domain level. You could also have a website set up for each domain on your server, and then just redirect to the page you'd like from that. You could also use the httpd.ini file to detect the domain and redirect to the appropriate page.
This is a small script that might serve the purpose.
You should place this at the top of header.php in your wordpress theme.
The script will not do anything if a domain is not matched so wordpress will load normal page. Haven't tested, but it should work.
<?php
$host = $_SERVER["HTTP_HOST"];
//Setup Domains Directory Names Here
$domain1 = 'domainname1.com';
$domain1_dir = 'domain1directoryhere';
$domain2 = 'domainname2.com';
$domain2_dir = 'domain2directoryhere';
$domain3 = 'domainname3.com';
$domain3_dir = 'domain3directoryhere';
//Redirects to directory depending domain.
switch (true){
case (preg_match("/$domain1/",$host)):
header("location:/$domain1_dir");
break;
case (preg_match("/$domain2/",$host)):
header("location:/$domain2_dir");
break;
case (preg_match("/$domain3/",$host)):
header("location:/$domain3_dir");
break;
}
?>

Categories