PHP - some proplems on detecting HTTPS Protocol - php

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.

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.

Secure or not secure URL PHP check

I'll try to make a script if the user is redirected to
http://example.com/?url=http://badsite.com
that the script reacts at the URL and displays an echo saying "Not secure", just like the Twitter and Google system, they also check if the URL is harmful.
I've found something, but this isn't what I was seaching for. Google couldn't help me, it gave me all vague answers.
<?php
$badsite = "http://badsite.com";
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == $badsite) {
echo "Secure, redirecting you";
} else {
echo "NOT SECURE";
}
?>
It isn't working as it should, it always displays "Not secure", while the link is secure. I think there's a error somewhere, but I need help fining it.
EDIT
<?php
$badsite = "http://badsite.com";
$host = $_GET['url'];
if($host == $badsite) {
echo "Secure, redirecting you";
} else {
echo "NOT SECURE";
}
?>
Ive tried, but it's still not working :(
Let me make the question a littlebit easier, how can I make it so if the url is
http://example.com/?url=Texthere
that there will be an echo on the page with "this is the page of texthere" and if "Texthere" isn't in the URL, that the echo won't be displayed?
Change your $host variable and if statement like that:
$host = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host != $badsite) {
echo "Secure, redirecting you";
}
This worked just fine for me (tested).
You need to include http:// into your variable, and your if statement was wrong.

How to correctly check if page was included? [duplicate]

This question already has answers here:
PHP: Check if a file is loaded directly instead of including?
(15 answers)
Closed 8 years ago.
I want to make sure that my pages are being included working index page. I would like to know what would be correct way of assuring that my page is being included instead of rendered by itself?
Right now I'm checking if there are at least 2 included files, but I'm not sure I'd it's behavior.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Included page and how I check is it's included
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
I'm not sure if you're talking about this:
include 'test.php';
If yes, then do a simple test like this:
test.php
$testVar = '1';
index.php
include 'test.php';
echo $testVar;
I have no idea what library you're using, so i hope this simple example will allow you to understand.

Strategy to pass data from included PHP file

The following code presents a way that I am currently rendering my pages through index.php. The problem is that I'm not sure how to re-think this so I can pass a page title before the template has been included.
How other way I could do this? This is just my index page, please ask if more code needed.
include($cms->GetTheme() . "/head.php"); This should get the Title information before being included, but I'm not sure how to pass data there from later included page.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Example page being included. The $this->SetTitle($module_name); I would use to set the page title.
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
There are echos all over the place. Try and limit the places where you do that by storing the output, rather than printing it all out straight away.
In your module for example, you could do $this->content = "You have been logged out..."
Then you can change the order of execution:
$cms->IncludeModule($_GET['page']);
include($cms->GetTheme() . "/head.php");
echo $cms->content;
include($cms->GetTheme() . "/foot.php");

php include file not found error

I have a question. I have this script
<?php
$GetPage= "index";
if((isset($_GET["page"])==true) && ($_GET["page"] != "")){
$GetPage = $_GET["page"];
}
?>
But I search on stackoverflow and google. But I can't find it. I want to include a error page when php can't find the file. How can I do that? I'm jut a starter with php.
Ow almost forgoten. I use this to include a part of my site:
<?php include ("include/$GetPage.php"); ?>
Thanks for reading !
<?php
//file_exists will eliminate the need for any of your other checks.
if(file_exists($_GET["page"])){
//Set the page to be loaded if it is found on the server
$GetPage = $_GET["page"];
}else{
//Show the user a 404 error message
header("HTTP/1.0 404 Not Found");
//OR
//Set the page to be loaded as your custom error page
$GetPage = "my_error_page.php";
}
//Include the page
include $GetPage;
?>
Are you looking for a 404 redirect? Or just load a custom error page into the document? Select the above based on what you wish to do.
$GetPage = $_GET["page"]; // validate string!!
if (!file_exists('include/' . $GetPage . '.php')) {
$GetPage = 'errorPage';
}
first check file is in folder (for injection) , ($file is full path of file.)
$path = "include";//your include folder path
if ( substr(realpath($file) , 0,strlen($path)) != $path || is_dir($file))
//error file not found
second check file is exist or not
if (!file_exists($file))
//error

Categories