Switch between mobile and full site - php

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

Related

How to create multi-regional website using wordpress

I'm trying to make a multi-regional website using WordPress. i'm working on url http://localhost/siteone I want when someone come from usa redirect to the localhost/siteone/usa/
I edit the function.php file of wordpress theme with code below, but getting error the url become localhost/siteone/usa/usa/usa/usa
// Detacting user location using ipstack
$json = file_get_contents('http://api.ipstack.com/check?access_key=ACCESSKEY&language=en');
// Converts it into a PHP object
$data = json_decode($json);
$loc = $data->country_code;
//NA = northamerica
//End of Decating user location
if($loc=="NA"){ header("Location: usa/"); }
This works very well sidealone but when I add in function.php in theme file not working and give me a error. what should I do. Should i use some session or anything eles.
It looks like it's trying to redirect multiple times, since it'll check the person's location, then redirect, then after redirecting it'll check again, and redirect again, etc.
What you could do is check whether "/usa" is already in the page URL, and if not, redirect, something like this could work:
if ($loc === "NA" && strpos($_SERVER['REQUEST_URI'],'usa/') === false) {
header("Location: usa/");
}
$_SERVER['REQUEST_URI'] is the URL of the current page, not including the domain.
For languages and create diferents regionals website, I'd recommend you this plugins (pro):
https://polylang.pro/ (just one paid)
enter link description here (anual fee)
Custom code based on language:
<?php
if(pll_current_language() == 'en') {
echo 'Only in english';
} else if(pll_current_language() == 'fr') {
echo 'Seulment en francais';
}
?>
If you prefer control language throught web server, use better: $_SERVER['HTTP_ACCEPT_LANGUAGE']
Link: https://www.php.net/manual/en/locale.acceptfromhttp.php
Regards!

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 make a m.mysite.com mobile site in 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
}
?>

Can you use PHP Session on a file called by another domain?

I have a php file on external-site.com like this:
<?php
session_start();
if($_SESSION['something'] == true) {
//do something
}
?>
This PHP file I include on a different website example.com like this:
<script src="http://external-site.com/session.js.php"></script>
When the visitor visits example.com he is redirected to external-site.com. Here I have this code:
<?php
session_start();
$_SESSION['something'] = true;
?>
And then the visitor is immediately redirected back to example.com.
Does this work? Because I am not violating the Same Origin Policy, right? I do not want to use the session on example.com itself. I only need it for external-site.com. So I do not want to transfer the session to another domain or anything like that.
If so, in which browser does it work and in which browser it does not?
Session are only available on the site which host the session calls.if you have two sites, they can't share natively session.
Edit : so if your session is opened on external -ite.com, you can't use it on example.com. in your case you need to use a SSO service.

URL rewriting, same URL across multiple PHP files

Hey so I have create a login system to a website and I wish to have this login appear when I type in my address. When I have typed in details and logged in, I wish to be redirected to another PHP file, but with the same address.... this way, All I need to do is type in my address if I am allready logged in and I will go to the site which requires login.
I have made a transaction happen identifing if the session is created, if it is, it redirects me to another page, but also to another URL. I tried googleing it, but couldn't find anything exact and straight forward.
Currently:
Login page:
www.example.com
Member page:
www.example.com/members
What I wish for:
Login page:
www.example.com
Member page:
www.example.com
The program structure should look like this.
index.php
if (user is logged in)
display dashoard
else
display login page
Since you are using PHP, make use of session functions. Thus, URL rewriting is no longer necessary.
Update
Assuming if you have file structure in PHP like this:
- index.php
- login.php
+ template
- login.php
- dashboard.php
You can do the following structure in index.php file.
define('IN_FILE', true);
if (isset($_SESSION['user'])) {
require 'template/dashboard.php';
} else {
require 'template/login.php';
}
In template/dashboard.php
if (!defined('IN_FILE')) {
exit;
}
// Then your HTML, PHP and whatnot
And in login.php
if (!isset($_SESSION['user'])) {
require 'template/login.php';
} else {
header('Location: index.php');
}
Change the code according to your needs.
This can be achieved using several approaches.
a) Use session to determine the current page, so if a user click on a link, create a session store the value and on page load read the session data and include the file accordingly.
b) Use URL parameter to determine the page (this is the most common approach). for example in index.php you can add more parameters like index.php?page=somepage and by reading the value using $_GET and including the PHP file accordingly.
There are some more way to achieve what you want to, for instance using javascript/jQuery this is possible.

Categories