Php Cname redirection - php

Recently I've been thinking about such a question: how to use php to recognize that a site is referenced with a CNAME record, and if so, redirect it to a specific document?

$_SERVER['SERVER_NAME'] contains the domain name of the site being served, which you can plug into dns_get_record(), filtered to get only CNAME records. So, if the domain name that was used to view the site is a CNAME, the result will not be empty, which you can use as a conditional for your redirect. I can't think of any use case where this would be desired, but something like this:
if (!empty(dns_get_record($_SERVER['SERVER_NAME'], DNS_CNAME))) {
header('Location: <whatever>');
exit;
}

Related

Address Bar not changing - PHP

I am using header('Location:') to redirect the user to another webpage. But, instead of overwriting the previous address on the address bar, the new one gets appended to the one already present. For example:
The address bar currently holds:
localhost/v2/admin
and there is header('Location:'.DIRADMIN.'login.php');
where DIRADMIN is a constant defined as
define(DIRADMIN,'localhost/v2/admin/');
Then, after the redirect, the new address bar would be
localhost/v2/admin/localhost/v2/admin/login.php
I thought it was because of the server, I am using. I was using Uniform Server Zero. But then I installed WAMP and the problem continues.
I am still a noob, I have no idea what is causing this and if the problem is because of mod_rewrite, then on both servers the module was active. I already checked some of the problems like
redirect-PHP header(Location:..)
among others. I even did a google search for this but to no end.
How can I solve this problem?
header_remove();
will not work as the first address was typed in manually and not set by header();
Without the http prefix, browser is trying to find the path relative to your current one
Add "http://" as so
header('Location: '.DIRADMIN.'login.php');
If you want URL to be relative to your domain root path, you can just add '/'
header('Location: /'.DIRADMIN.'login.php');
You should always try to use a relative path - That way, if you change your domain, your code still works.
Define
define('DIRADMIN','localhost/v2/admin/);
As
define('DIRADMIN','http://localhost/v2/admin/');

(PHP) header(Location: ...)

Is there a way I can change the header instead of adding on?
For example
Creating a user
create_user.php (a form to action.php) -> action.php (switch case which will call another function) -> user_controller.php
So from user_controller.php, upon successful creation, i used the header method to bring the user to another location, view_user.php.
So now the url header is like
http://localhost/dct/action.php/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
but i want it to be
http://localhost/dct/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
without the action.php. How do i go about achieving that?
You will need to use URL rewriting - this depends on your setup. You can either do it at the web-server level, or application-level.
Two ways :
save your "base_url" website (http://localhost/dct/) in parameter and call header('Location: ' . $baseUrl . '/veiw_user.php?...)
Simply add a / before your URL (example: header('Location: /view_user.php?...'), and it will redirect from the root of your website, but in your case, it will redirect to http://localhost/view_user.php?....
The first way is better for your case, but you have to update the parameter when you publish your website in production environment.

ID variable from URL

Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);

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.

Redirect if link reached from external site

I have a PHP page on a website that I'd like to be accessible only from another page on that website.
If a user clicks a link to the page, or types the page's address, and does not come from the site's domain, I'd like the page to redirect the user to the index.
Any ideas?
What you could do is use sessions.
make the index set a variable
$_SESSION['visitedIndex'] = TRUE;
and testing for it in the other pages:
if(!$_SESSION['visitedIndex']) {
header('location: ....');
}
make sure you do this before the first echo.
You could also create an internal service using a $hash = timestamp + internal secret key or your paricular rule.
First page has a link
http://www.samesite.com/page_2.php?param=hash
Second page decodes the hash and check the timestamp against a given interval. Otherwise it refuses the display.
As only you know the internal key is impossible to fake.
Check 'Referer' field?
It's easily hackable, tho. The more reliable way is to check if the used had no active session (if your site assigns them to visitors).
Use the referer fo this:
if ($_SERVER['HTTP_REFERER'] != "...") {
header("LOCATION: othersite");
}

Categories