Finding status of website with Cloudflare DDoS protection - php

Trying to check the status of an website using the following code:
<?php
$url = 'https://shop.bbc.com/';
list($status) = get_headers($url);
if (strpos($status, '404') !== FALSE) {
echo '404 error';
} else {
echo $status;
}
?>
However since the website is protected by cloudflare it gives a 403 forbidden error when I try to access it. Is there any known workarounds for this problem?

Related

Detect if localhost/Xampp/Laragon is running

I am building a project where an online php script needs to loads files from the local server (this is not a public website).
Is it possible to detect if the local server is running or not and display a message. Something like this (C# Check If Xampp Server/Localhost is Running) but with php.
gethostbyname will not work.
$domain = '127.0.0.1/info.php'; // or $domain = 'localhost/info.php';
if (gethostbyname($domain) != $domain ) {
echo 'Up and running';}
else {
echo 'Run xampp first';
}
This will not work too
$file = '127.0.0.1/info.php';
$file_headers = #get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
echo 'Run xampp first';
}
else {
echo 'Up and running';
}
This is not a URL, it's a local filename:
$file = '127.0.0.1/info.php';
It lacks the leading protocol specifier, so it's looking for a file named info.php in a directory named 127.0.0.1.
You will need:
$file = 'http://127.0.0.1/info.php';
Then, assuming that http://127.0.0.1/info.php is a valid URL that will be served if the web service is running, you can use file_get_contents() to try and load the page. This will issue a warning and return false on failure.
if (#file_get_contents('http://127.0.0.1/info.php')) {
echo "server is up";
} else {
echo "server is down";
}
You could also use get_headers() as in your exmaple, but note that if you get a 404 Not Found, that still means the server is up and running.

I'm always getting 301 HTTP header code even if the site doesn't exist (PHP)

I'm trying to figure out if a Instagram User exists or not. If the user site doesn't exist, it should give me a '404 Not Found' in the headers, but i'm always getting a '301 Moved Permanently', even if the user doesn't exist!
My browser gives me a 404:
https://i.imgur.com/to38Sb2.png
Can u help me? This is my code:
<?php
$url = "https://www.instagram.com/asdsdfsvxd"; //This user doesn't exist
echo $url;
$file_headers = #get_headers($url);
echo $file_headers[0];
if($file_headers[0] !== 'HTTP/1.1 404 Not Found') {
echo "exists";
} else {
echo "not exists";
}
?>
Solution:
Dont forget the trailing / (slash).
$url = "https://www.instagram.com/asdsdfsvxd/";
Output:
$ php test.php
https://www.instagram.com/asdsdfsvxd/HTTP/1.1 404 Not Foundnot exists

PHP - some proplems on detecting HTTPS Protocol

i am getting an problem on loading my Template and i had tried testing my template on XAMPP and the same problem happened again
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator at admin#main-hosting.eu to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error
log.`
it doesn't show anything at first just loading page and then that error no more... please help but on XAMPP it just keeps loading my page
but after some time when i was trying to resolve the problem, found it and tried solving it but i need some help please on fixing the code which caused it before
$Domain = $_SERVER['HTTP_HOST'];
$Path = $_SERVER['PHP_SELF'];
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS']))
{
echo '<script type="text/javascript">window.location.assign("https://' .$Domain.$Path. '");</script>';
}
else
{
echo '<script type="text/javascript">window.location.assign("http://' .$Domain.$Path. '");</script>';
}
please some help ???
The reason is because each time your page is loading the script is running and refreshing the site endless. You need to know if the page have already been redirected.
example:
<?php
if (!isset($_GET['r'])){
$Domain = $_SERVER['HTTP_HOST'];
$Path = $_SERVER['PHP_SELF'];
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS']))
{
echo '<script type="text/javascript">window.location.assign("https://' .$Domain.$Path. '?r=https");</script>';
}
else
{
echo '<script type="text/javascript">window.location.assign("http://' .$Domain.$Path. '?r=http");</script>';
}
}
?>
I do not understand the point of this.. And I would recommend you to use Location instead. Example:
header('Location: http://www.example.com/');
This is a very poor way to reload a page.
The code below reloads the page infinitely until the url is modified...
To understand this, run the code below...
$Domain = $_SERVER['HTTP_HOST'];
$Path = $_SERVER['PHP_SELF'];
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
echo 'HTTPS AVAILABLE';
echo '<script type= "text/javascript">window.location.assign("https://' . $Domain .$Path. '");</script>';
} else {
echo 'HTTPS NOT AVAILABLE ';
echo '<script type="text/javascript">window.location.assign("http://' . $Domain .$Path.' ");</script>';
}
while the pages keeps loading, modify the code to this
$Domain = $_SERVER['HTTP_HOST'];
$Path = $_SERVER['PHP_SELF'];
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
echo 'HTTPS AVAILABLE';
echo '<script type="text/javascript">window.location.assign("https://' . $Domain . '");</script>';
} else {
echo 'HTTPS NOT AVAILABLE ';
echo '<script type="text/javascript">window.location.assign("http://' . $Domain .' ");</script>';
}
once saved this will redirect you to https://localhost or http://localhost
if running on your local machine.
The window.location.assign() reloads the page infinitely if the url is the same.

Mobile Detection and cURL redirect giving 404

So I have a desktop (www.example.com/friends.php) and separate mobile site (m.example.com/friends.php). Not all pages on the desktop site exist on the mobile site.
The Goal: I am detecting a mobile device, then if it proves true, I want to capture the desktop URL after the .com (friends.php), and substitute the mobile domain "m.example.com + $pagename". I then want to detect if the page exists on the mobile domain via cURL (see below). If it exists, I want it to redirect to the corresponding mobile page, if it doesn't exists (returns 404), it should redirect to the base mobile domain.
I currently have the function set to alert if a mobile page is found or not based on the 404.
The issue: when I testa page I know does not exist, it alerts "mobile page found" always, and then will redirect to the mobile site 404 page. What am I missing here? I've stared at this for hours.
The Code:
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);}
if(isMobile()){
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$pagename = curPageName();
$mobileurl = 'http://m.example.com/' + $pagename;
$curl = curl_init($mobileurl);
curl_setopt($curl, CURLOPT_NOBODY, true);
$result = curl_exec($curl);
if ($result !== false) {
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 404) {
echo "<script type='text/javascript'>alert('mobile url not found');</script>";
header("Location: http://m.example.com/");
} else {
echo "<script type='text/javascript'>alert('mobile url found');</script>";
header("Location:" + $mobileurl);}
} else {
echo "<script type='text/javascript'>alert('mobile url not found');</script>";
header("Location: http://m.example.com/");
}
} else {
return false;
}

how to check if ssl exists on a webserver through php?

I have this function in a class:
function enable_ssl() {
if ($_SERVER[HTTPS] != "on") {
$domain = "https://".$_SERVER['HTTP_HOST'] . "/" . $_SERVER['SCRIPT_NAME'];
header("Location: {$domain}");
}
}
The problem is that when the server doesn't have SSL installed and I have this function initiating the page redirects to a 404 page. I was wondering how I can have this function work only when SSL is installed and working?
Is it possible?
Thanks.
P.S.: I did some Google research but couldn't find much of anything.
On a *nix server, you could try parsing the output of netstat -A inet -lnp for a web server listening on port 443. Kinda clunky.
Better option, I'd say, is to make it a configuration option for the user. Let them tell your app if they've got HTTPS enabled.
two ideas
Setup a socket connection to port 443 and see if it connects.
Read through an apache config file and see if there's anything listening on that port
Extension Loaded!
http://php.net/manual/en/function.extension-loaded.php
e.g.
if(!extension_loaded('openssl'))
{
throw new Exception('This app needs the Open SSL PHP extension.');
}
You can try to connect to the server using curl. However, I would also try to do a config option. If you use the below, make sure you don't cause an infinite loop.
function ignoreHeader($curl, $headerStr)
{
return strlen($headerStr);
}
$curl = curl_init("https://example.com/");
curl_setopt($curl, CURLOPT_NOBODY, TRUE);
curl_setopt($curl, CURL_HEADERFUNCTION, 'ignoreHeader');
curl_exec($curl);
$res = curl_errno($curl);
if($res == 0)
{
$info = curl_getinfo($curl);
if($info['http_code'] == 200)
{
# Supports SSL
enable_ssl();
}
}
else
{
# Doesn't.
}
I use xampp as my development server on my laptop. I have yet to set up a SSL connection on xampp. My production server does have SSL enabled and also has a valid cert.
I noticed that $_SERVER['HTTPS'] does not exist on my xampp development server, but does exist on my production server.
I am assuming (perhaps incorrectly) that if $_SERVER['HTTPS'] is not set, SSL is not enabled on the server.
<?php
if (isset($_SERVER['HTTPS')) echo 'SSL Exists'
else echo 'No SSL'
?>
I just use file_get_contents to try and open the same file via https and if it was successful, force a redirect...
function IsHttps()
{
return
(!empty($_SERVER["HTTPS"]) && (strtolower($_SERVER["HTTPS"])!=="off"))
|| ($_SERVER["SERVER_PORT"]==443);
}
if (!IsHttps() && extension_loaded("openssl"))
{
$target = "https://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
ini_set("allow_url_fopen", true);
if (file_get_contents($target)!==false)
{
header("Location: https://".$_SERVER["HTTP_HOST"].$_SERVER["URL"]);
die();
}
}
I have used the code below
$file ="https://mydomain/index.php";
$file_headers = #get_headers($file);
$DomainName=(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found')?'http://mydomain':'https://mydomain';
This basically checks if a file can be found (accessed) via HTTPS.
This is how wordpress does it:
<?php
function is_ssl() {
if ( isset($_SERVER['HTTPS']) ) {
return true;
if ( '1' == $_SERVER['HTTPS'] )
return true;
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
return true;
}
return false;
}
?>
You can use your validation in between 'return true' stuff! Go to:
http://tutes.in/2012/02/13/check-if-ssl-exists-on-a-webserver-through-php/
for explanation and more detailed source code.

Categories