Detect if localhost/Xampp/Laragon is running - php

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.

Related

Finding status of website with Cloudflare DDoS protection

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?

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.

Is there a way to determine the upstream web server with PHP?

I'm developing a PHP web application that needs to send out a file in a server specific way, like so:
<?php
$server = get_upstream_web_server();
if ($server === 'nginx') {
header('X-Accel-Redirect: smiley.png');
}
else if ($server === 'apache') {
header('X-Sendfile: smiley.png');
}
else {
echo file_get_contents('smiley.png');
}
Is there a way to get the name of the upstream server like the get_upstream_web_server() in the above example?
See $_SERVER['SERVER_SOFTWARE'].

Functions not working on IIS

I am using this function to redirect to portfolio after user log in...
function redirect($destination)
{
//handle url
if (preg_match("/^https?:\/\//", $destination))
{
header("Location: " . $destination);
}
// handle absolute path
else if (preg_match("/^\//", $destination))
{
$protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
$host = $_SERVER["HTTP_HOST"];
header("Location: $protocol://$host$destination");
}
// handle relative path
else
{
// adapted from http://www.php.net/header
$protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: $protocol://$host$path/$destination");
}
// exit immediately since we're redirecting anyway
exit;
}
On using it produces SSL connection error in chrome:
Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.
in firefox
An error occurred during a connection to localhost:63077.
SSL received a record that exceeded the maximum permissible length.
(Error code: ssl_error_rx_record_too_long
Please don't tell me the problem...
tell me solutions or alternative
I am having a windows azure account...
It's not even working there....
Kind Regards
Vishal
PS:I know it's going to cost a lot of time ....
I really need this for my imagine cup project ..
Seems you are having issues getting the correct protocol. I'm not sure if this will work on IIS, but I generally use the following on Linux - can't imagine why it wouldn't work:
function getProtocol()
{
return $_SERVER['SERVER_PORT']=='443'?'https://':'http://';
}
That should remove most of the complexity in your code?

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