PHP find text in self - php

if (strpos(file_get_contents($_SERVER['SERVER_ADDR']),'<html>')) {
echo 'true';
}
I'm trying to check if the current page contains a string, and if so echo true. I've tried this however it doesn't work.

$_SERVER['SERVER_ADDR'] contains a completely different value than you seem to be expecting:
The IP address of the server under which the current script is executing.
To get a working link to the current page:
$currentUrl = $_SERVER['HTTPS'] ? 'https://' : 'http://';
$currentUrl .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
However, this becomes a giant mess, if you're requesting your own page from that page's code you'll get a clusterf*ck of endless recursive retrievals which will eventually blow up your server. I would 'recommend' not downloading the page in which you are doing this.

Related

Get current URL with PHP and change a part of it for a new url

I am working on a WordPress website for a client. I want to create a switch element through a current URL change.
For instance;
https://mywebsite.com/shop/CUSTOMER
should change to
https://mywebsite.com/shop/OWNER
echo $ _SERVER ['REQUEST_URI']; ?>
I have found a php code that provides me the URL of the current page. So if the visitor is on the /shop/customer page, I've found a way to create a shortcode and add my own string to it. But it creates a new link like; /shop/customer/owner
I want to replace customer in owner, not adding owner to the current link. Because it has to be a dynamic solution. For example, if the visitor is on https://mywebsite.com/shoppingcart/customer, they should change this to https://mywebsite.com/shoppingcart/owner
Sorry for the bad english, hope you understand what i need.
Regards,
Rick
May be this could work!
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$change_url = str_replace("customer","shopping",$actual_link);
echo $change_url;

Accept connections only by short link

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.

Echo the Requested URL in PHP

I am not even A PHP newbie. But I need to call BS on a outside consultant who has come to my company and told me something was impossible.
Is it possible in PHP to simply create a webpage that displays the URL requested by the user.
In other words: rather than echoing/Printing "Hello World"
I would like the page to print the URL that the user requested.
This can be accomplished with:
echo $_SERVER['REQUEST_URI']--
for a full list:
print_r($_SERVER)
Another useful one is $_SERVER['HTTP_HOST']
If you're going to do a redirect, you have more footwork to do, but a straightforward solution would be:
header("Location: {$site}?original_request={$_SERVER['REQUEST_URI']}"); - and then handle the rest in the page you redirect to.
I think what you are looking for is the full url. This can be done with.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $url;
Just incase the port is included.
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$url = "http://".$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI'];
Here ya go:
echo $_SERVER['PHP_SELF'];
In a sense, both you and the consultant are correct. In most cases, yes you can get the exact URL. In many cases, the URL will be approximate. In some cases, you might not get anywhere close to the requested URL.
Here are some barriers to building the exact URL:
The #fragment isn't sent
You don't know if :port was present (so you can't know whether to add it or not)
You don't know what outside PHP rerouting was done (eg in .htaccess)
You could get around these by having a hidden input variable that is filled by Javascript with the full URL, which is then posted. But then the argument is "well what if Javascript isn't turned on."
So, I'd say "yes you can" is the practical/pragmatic answer, while "no you can't" is the academic answer.

getting web url address to detect web proxy

I'm trying to blocked website based proxy's. These are normally in the format of:
http://3.hidemyass.com/ip-8/encoded/Oi8vZ29kbGV5ZGVzaWduLmNvLnVrL0xDcmVkaXJlY3QvZnVuY3Rpb25zL2Z1bmN0aW9
My theory of blocking these is to get the URL of the address bar and check that it's actually direct access to my site, rather than visiting via a website proxy.
However, when i try to visit my site and attempt to capture the url of the user it still reports that its my sites url.. not this web based proxy one.
I've tried the following ways of detecting it:
$url= $_SERVER['HTTP_HOST']; //get the url
$url = $_SERVER["SERVER_NAME"];
any ideas on how to resolve this?
UPDATE
Ok i've rewrote part of this, however it always seems to be returning false... the $url is being passed correct as i can echo this out within the function. However it doesnt seem to be matching and returning false
<script>
var url = window.location.href;
<?php $url = "<script>document.write(url)</script>"; ?>
</script>
<?php
//
function checkURLIsSafe($url){
if(preg_match('/www/',$url)){
echo 'true';
} else {
echo 'false';
}
}
checkURLIsSafe($url);
?>
PHP runs on the server. It can only see the URL that was requested from it.
hidemyass.com will be requesting the normal URL from your server. There is no way to tell what URL the browser requested from hidemyass.com.
Approaches you could take include:
Checking the source ip against a list of known proxies
Using client-side JavaScript to read location.href
You cant do it with PHP only. What you can do is to check window.location.href with javascript, and if it's incorrect, send ajax request to server, which will block IP address.

Retrieving referral url application

Let say we've the following
Objective : User will post certain exact URL $refere to lock viewing text content and only be allowed for view if the viwer is coming from the same exact URL $refere.
$refere = "http://www.site_site.com"; // User will post it
$r = $_SERVER['HTTP_REFERER']; // To get real referral
and i want to do the following
<?PHP
if(stripos($r, $refere) == false){
echo "Wrong";
} else { ?>
echo "Go";
}
?>
It always gives me $r = $_SERVER['HTTP_REFERER']; blank ! so does it deprecated on any PHP version 4 or 5 whatever !
Also
what is the user posted $refere like https:// or missed www. or only posted site_site.com while the $r = $_SERVER['HTTP_REFERER']; showing www.site_site.com
so can anyone help me to adjust this code to be working fine no matter the user posted the $refere link fully or only site_site.com.
The $_SERVER['REFERER'] variable will only be set when you click a link to your page from another page and if the browser (or an eventual proxy or firewall you're on) isn't removing the referer header.
To your second question: do some string comparisons. The functions strpos() and substr() will be of great help.

Categories