Add restriction to a domain - php

I build 2 website with a single codebase that have the same database and deployed in two different web sites.
I would like to restrict another domain 2 from accessing domain 1 and add function that if the domain is domain 2 replace it to domain 1, but i dont know what function to add
<?php
if($_SERVER['HTTP_REFERER'] !== 'abc.com'){
//add funtion to restrict domain 2
}
elseif($_SERVER['HTTP_REFERER'] == 'cde.com');{
// add function here to change domain 2 to domain 1
}
?>

Related

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
}
?>

How to check Referrer url is Domain OR Sub domain in PHP script

I want to check the Referrer url is Main Domain or Sub domain, domain can be anything like .com , .net ,.co.in , in .. Sub Domain also can be different type… any one can suggest idea in PHP script …..
For e.g if referrer url is http://google.com is the Domain, http://in.google.net is the sub domain
This should do the trick. (Haven't tested the code - so please take as a suggestion only)
<?php
$fromOtherDomain = !preg_match('/^https?:\/\/' . preg_quote($_SERVER['HTTP_HOST']) . '/i', $_SERVER['HTTP_REFERRER']);
?>
I got the Answer.
The requirement has changed and I now match all subdomains belonging to same domain (*.example.com).
So I have used the following code:
if(!preg_match('/[A-Za-z0-9].example.com/', $_SERVER['HTTP_REFERER'])){
echo 'Sub Domain';
}else{
echo 'Main domain OR sub domain not belongs to example.com'
}

create subdomain programmatically in PHP

I am on shared hosting and on a add on domain.
I need to create subdomain for each user of my website like if the username is jeff then he should have a url jeff.mydomain.com.
How can I create it programmatically using PHP?
There's two parts to this. Firstly you'll need to setup a wildcard dns entry.
Once you've got that setup you should have all your requests pointed back to a single domain. From there you can then use php to figure out which domain you're currently on:
$domain = $_SERVER['HTTP_HOST'];
$base = 'mydomain.com';
$user = substr($domain, 0, -(strlen($base)+1));// the user part of the domain
if(!empty($user)) {
$user = sanatiseUser($user);
require_once $user.'.php';
}
You need to set apache to listen for all domains coming into a specific IP.
You then need to setup a wildcard DNS entry to point *.domain.com to that IP.
Then inside your app, use $_SERVER['HTTP_HOST'] to determine which user to load.

1 domain 2 websites

There are 2 different websites in 2 directories ..path/siteA/ and ..path/siteB/ . I need to load one of them on domain example.com depending on their country they are visiting from.
It can't be www.example.com/siteA it must be www.example.com .
Is it posible?
Edit: found the solution.
Configure Your webserver to listen on two different ports, one pourt should serve content from /path/siteA and the second one from /path/siteB.
Next step is to configure Pound depending on the location of the user (IP geolocalisation) and You're on Your way
It is usually done with geo-location.
You use a redirect on example.com/index.php that redirects to example.com/pathA or example.com/pathB depending on their ip
use the header() function to redirect :)
$ip = $_SERVER['REMOTE_ADDR'];
if(...) // check location of IP
{
header("Location: /pathA");
}
else
{
header("Location: /pathB");
}
http://php.net/manual/en/reserved.variables.server.php
http://au.php.net/manual/en/function.header.php
Edit:
Based on comment, this is what you need: mod_Rewrite: Filter specific pages by IP and redirect them
Yes it is possible. First pages you have to ask the language or show the default language.
This is done by two methods:
Each language can be lines or words assigned in separately files with each variables. By using the variable calling we can the get the language parameter.
Each language can be lines or words assigned in database with separately table.

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