Accept connections only by short link - php

I want to make my web site so that it can only be accessed from visitors that accessed via a short link, and block them when they access my web by entering the address.
how can i do it on .htaccess?

In php you can check for the previous URL by using $_SERVER['HTTP_REFERER'].
So you can compare the previous url with $_SERVER['HTTP_REFERER'] to verify the visit.
if ($_SERVER['HTTP_REFERER'] == 'your-short-url') {
//redirect to home page
} else {
//display page not found;
}

Store shortlink addresses in the DB and then validate them on server site. The second approach is to check if the domain is comming from your shortlink provider, f.i. bit.ly
$_SERVER[HTTP_HOST] == 'bit.ly';
To check current address in php:
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
current browser URL using JavaScript:
window.location.href
This will return you the string with URL you currently see in your browser.
If you choose DB use approach compare each element in database with your current url and display website only if they are matching, coming from the same domain or any condition you want.

Related

How to get referrer url and display it in PHP

I need a code to display referrer URL (full URL if possible, like example.com/1234). if full URL is not possible, then at least the domain is fine. Also, I want to see if the referrer is NOT coming from somewhere for example: if referer is NOT example2.com then //do action
I have a code but it's not working properly. If you can help, that would be great. Thanks
<?php
// Check if Referral URL exists
if (isset($_SERVER['HTTP_REFERER'])) {
// Store Referral URL in a variable
$refURL = $_SERVER['HTTP_REFERER'];
// Display the Referral URL on web page
echo $refURL;
} else {
if (( $refURL == !'https://example2.com' ) ) {
// sorry, you didnt come from example2.com, you NEED to come from example2.com}
?>
This doesnt seem to work. Also, http refer only displays domain of referrer without path like example.com/1234/321. If its possible to get the full URL please share, otherwise i can do without it.

How to retrieve original url in a redirected page using PHP

Let say I have redirect rule on url example.com which redirect to newurl.net.
How can I get original url on newurl.net in order to be used and handled with some details or just to be printed on new destination.
Thank anyone for help.
If it's not external You can use
$_SERVER['HTTP_REFERER']
And it will give you the referer which is what you want.
See Acquiring referral after a PHP redirect for more informations.
But if you are trying to get to another website, the browser will overwrite the headers so you need to do it some other way.
You can store it in session.
//Save it in your first website
$_SESSION['REFERER'] = $_SERVER['HTTP_REFERER'];
And then in the other website :
//Use it in the other
$referer = '';
if (isset($_SESSION['REFERER'])) {
$referer = $_SESSION['REFERER'];
unset($_SESSION['REFERER']);
}
You could also pass the referer as a parameter:
header('Location: http://newurl.net?original_referer=' .$_SERVER['HTTP_REFERER']);

$_SERVER['HTTP_REFERER'] not working on IE

I have the following code to determine the URL used to load a page, it works in all browsers except IE.
Is this a known issue?
if(isset($_SERVER['HTTP_REFERER']))
{
//correct domain:
$domain=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false )
{
}
else
{
echo $domain['host'];
}
}
Is there a different way to get the URL that the user is using? Essentially I need to know what URL the user has entered to determine what to display on the screen.
Is this a known issue?
Yes:
'HTTP_REFERER'
The address of the page (if any) which referred the
user agent to the current page. This is set by the user agent. Not all
user agents will set this, and some provide the ability to modify
HTTP_REFERER as a feature. In short, it cannot really be trusted.
Also the above differs from what you want:
Is there a different way to get the URL that the user is using? Essentially I need to know what URL the user has entered to determine
what to display on the screen.
REQUEST_URI is what you are looking for:
'REQUEST_URI'
The URI which was given in order to access this page;
Source: http://php.net/manual/en/reserved.variables.server.php
Also see: Get the full URL in PHP

PHP - referred redirect script

I have a tracking link (poly.wtf) .
I'm trying to make a php page that check the referred if and to make this condition if the visitor came from my tracking link (poly.wtf) it will redirect him to page X
if the visitor didn't come from my tracking link (anywhere else) or just type the url through the browser it will redirect him to page Y
I'm tried to use this script PHP :
<?php
$domain = 'poly.wtf';
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match($domain ,$referrer)) {
header('Location: https://www.google.com/');
} else {
header('Location: https://www.yahoo.com/');
};
?>
but it doesn't work because for some reason it doesn't capture for me the refer here is example of redirect from poly.wtf link to the PHP page :
http://poly.wtf/5d9e31ce-bd7d-4500-bc4e-b7e7f7ddcacc
^^ in this case it should redirect me to google but it doesn't , what do you think is the problem?
You shoul enclose your pattern in some delimiter like:
$domain = '/poly.wtf/';

Automatically redirect to the PREVIOUS BROWSED page

I have read some stackoverflow hyperlinks about how to redirect to the previous browsed page after a user logs in or creates a new post in a forum. I have found that most of the answers mentioned about using AJAX or javascript or the like, because most of the answerers think that it should only happen on the client side. Furthermore, most of their codes provided helpfully advised how to redirect to a certain page like index.php after log-in.
For me, what i really want to do now is how to redirect to the previous browsed/visited page after clicking the submit button of the form by using PHP (PLEASE FORGET ABOUT AJAX OR JAVASCRIPT NOW). I don't redirect users to any certain assigned page, but to the page immediately before it. For example, a user visits the index.php, then clicks a hyperlink on it to go to the posts.php page to view the new posts, and then he's interested in the forum, he then goes to the login.php page from the http://domain.com/posts.php?qid=7, for instance, to sign in so that he can reply to the post if he wants to or does something else. In this scenario, after he logs in, the forum would *REDIRECT* him to the immediately previous posts.php page, which is http://domain.com/posts.php?qid=7. That is what I want to code now.
And this is the function I just created:
function str_url($a, $b) {
return substr($a, 0, strpos($a, $b));
}
function self_url() {
if(!isset($_SERVER['REQUEST_URI'])) {
$self = $_SERVER['PHP_SELF'];
}else{
$self = $_SERVER['REQUEST_URI'];
}
$secure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s":"";
$protocol_secure = str_url(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$secure;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$_SESSION['previous'] = $protocol_secure."://".$_SERVER['SERVER_NAME'].$port.$self;
header("Location: " . $_SESSION['previous']);
}
All I have to do is to place the functions in the head of the document and the self_url(); anywhere within the PHP code after the form checking and submission conditionals that I want.
The problem is that the function self_url(); just checks the domain.com/posts.php to redirect the logged-in users to (it truncates the rest part of the URL), but not the whole long URL of the previously visited page like http://domain.com/posts.php?qid=7 (in which the id=7 is the name=value pair of a certain post) he just visited before he signed in.
Can you help me to isolate the reason, please? Or If you have any handy PHP function to replace mine, I'll appreciate it.
The previous URL might be visible to you via $_SERVER['HTTP_REFERER'] so you could just take this as your redirection target. But this is not very reliable - from the docs:
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents
will set this, and some provide the ability to modify HTTP_REFERER as
a feature. In short, it cannot really be trusted.
The better approach would be to let the previous script insert a GET parameter of itself so your redirection script knows where the user was before.
Well, you could store every visited page a cookie or session and just read it from there but that doesn't make much sense in my opinion.
//edit some code:
if(!isset($_SERVER['REQUEST_URI'])) {
$self = $_SERVER['PHP_SELF'];
}else{
$self = $_SERVER['REQUEST_URI'];
}
Just append ?redir=$self to the action of your form.

Categories