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'].
Related
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 have a product(Web Application), 5-10 clients are asking same application to install in their own domain. i don't have any problem to install, but some clients(who is having technical knowledge) are installing in 2, 3 domains without paying for me. How can i restrict them for only one domain. I mean some authentication process we have to follow, which gives access to clients to install.
could anyone give me any suggestions.
Thanks.
Make a function that sends a request to your server, in example:
function checkSerial($serial = '1111-1111-1111-1111') {
$server = file_get_contents('http://server.your-domain.com');
if (json_decode($server)) {
$result = json_decode($server);
if ($result[0] == 'valid') return true; else return false
} else return false;
}
and something on server side (index.php):
if (isset($_GET['serial'])) {
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' && !empty($_GET['serial']) && $_GET['serial'] == '1111-1111-1111-1111-')) {
$result = array('valid');
} else $result = array('invalid');
} else $result = array('invalid');
echo json_encode($result);
This is just a quick demo how you can implement such a thing!
I am currently developing a PHP application that is (hopefully) going into production use soon.
What I'm needing help with is detecting what URL the app is being accessed on ie dev.local, testing.domain.com or app.domain.com and then using the correct MySQL DB, ie app_test for dev and testing and app_prod for the production server.
Along with that, I also want to be able to modify the internal URLs to match (several emails are sent that also need to be tested with the correct URL).
I remember seeing some stuff about it before but am not able to find it any more.
Get full url of page
function request_url() {
$result = '';
$default_port = 80;
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']=='on')) {
$result .= 'https://';
$default_port = 443;
} else {
$result .= 'http://';
}
$result .= $_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != $default_port) {
$result .= ':'.$_SERVER['SERVER_PORT'];
}
$result .= $_SERVER['REQUEST_URI'];
return $result;
}
I think you will be enough: $_SERVER['SERVER_NAME']
Easy way to do that ......
Define environment constants in constants.php file
// constants.php
define('ENVIRONMENT', 'development');
//define('ENVIRONMENT', 'production'); // uncomment this when your going to live your project
define general functions in general.php
// general.php
include "constants.php";
function is_production()
{
if(ENVIRONMENT == "production")
{
return TRUE;
}
return FALSE;
}
function is_development()
{
if(ENVIRONMENT == "development")
{
return TRUE;
}
return FALSE;
}
Now you can us that functions in your database connection files and select your database and base url
// in db.php
include "general.php";
if(is_production())
{
$conn = mysql_connect("host1","username1","password1");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://domain.com');
}
else if(is_development())
{
$conn = mysql_connect("host2","username2","password2");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://testing.domain.com');
}
Now You can use that BASE_URL constant and you have database connection as you want
This general overview but you can implement in your project as your standered.. :)
Can someone help me with my if statement? I am trying to make two checks when people visit my website. 1st one checks the connection type if its not a Broadband connection it sends the header redirecting to google. The 2nd one checks to see if the ip address is a known proxy if so it sends the header directing to google. My problem is no matter if its a proxy or not it always sends the header, and same for the connection type no matter which one it sends the header. What am I doing wrong?
HERE ARE THE OPTIONS FOR $TYPE.
$type = "Corperate";
$type = "Dial-Up";
$type = "Broadband";
//Block based on connection type
if ($type != "Broadband") {
header('Location: http://www.google.com');
} else {
//Do Nothing
}
HERE ARE THE OPTIONS FOR $PROXY.
$proxy = "Suspected Network Sharing Device";
$proxy = "Known Proxy";
//Block based on proxy
if (strlen($proxy) > 0) {
header('Location: http://www.google.com');
} else {
//Do nothing
}
Dont both your options of $proxy have length>0 ?
I think it should be
if($proxy != "known_proxy"){
header('Location:http://www.google.com')
}
Use
if (!empty($proxy)) {
header('Location: http://www.google.com');
}
instead.
Also, I'm not even sure that is your problem since both proxy "options" are not empty strings, therefore have a strlen() > 0.
The only way that if statement will return false is if you were to do $proxy = ""
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.