Very simple issue we cannot resolve. The following code does not redirect properly:
$location = str_replace("http","https",$this->page->current_url);
header( "Location: ".$location );
I get a 400 Error: "Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.Instead use the HTTPS scheme to access this URL, please."
How can I redirect in PHP from a page served over HTTPS to a page served over HTTP for scenarios such as secure login and then redirect to nonsecure app page?
try this, for me works fine
<?php
if ( $_SERVER['HTTPS'] )
{
$host = $_SERVER['HTTP_HOST'];
$request_uri = $_SERVER['REQUEST_URI'];
$good_url = "http://" . $host . $request_uri;
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: $good_url" );
exit;
}
?>
This will work for http to https and https to http (use some tweaks to check)
First check whether the https is ON on your hosting if yes than redirect the URL to https using below mentioned function in PHP
function httpToHttps()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_UR
I"];
}
return $pageURL;
}
if( isset($_SERVER['HTTPS'] ) ) {
header( "Location:foo.php");
Not sure if this can cause such a problem, but you should add an exit after calling the header function. This makes sure that the following code will not be executed and the HTTPS request itself returns no data. The header function should be called before anything is returned.
$target = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('Location: '.$target, true, 303);
exit;
Duh - I have no clue why I did not look at $location. It contained http//localhost:443/[...] which is invalid. I simply remove the :443 and it works fine.
Related
I have this PHP code to force HTTPS (I am aware I could also use .htaccess).
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
header("Status: 301 Moved Permanently");
header(sprintf(
'Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
));
exit();
}
}
This changes this URL
http://website/foo/
To
https://website/index.php
But changes this URL
http://website/foo/bar?this=that
To
https://website/foo/bar?this=that
Why is this happening, and how can I get only change to HTTPS without losing the URL
I have a PHP script which receives user and pass as a URL parameter and stores this in a database.
In order to use this script I have to access
http://ipadress/script.php?user=testuser&pass=1234
that's the IP address of a Linux machine.
What changes should I make in order to be able to change from http to https? I have to use SLL certificates or is there a solution which allow me to do this from my PHP script?
Can you offer me some hints, please ?
I hope this helps!
$redirect= false;
if (!isset($_SERVER['HTTPS'])) {
$redirect= true;
} else {
if ($_SERVER['HTTPS'] != "on")
$redirect= true;
}
if ($redirect) {
$url = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$url);
exit();
}
I've the following code which I got from somewhere and it doesn't appear to be working:
function http() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
return $http;
}
Can someone help?
What I am attempting to do is return the website protocol when I type in $http
ex:
Website URL including Protocol
I've got the $websiteurl down, I just can't seem to get it to echo http vs https. I don't know much about functions, so I'm not sure how to troubleshoot it.
The http is a function, so you dont call it like a variable using $
try:
function http() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
return $pageURL; // <-changed
}
Website URL including Protocol
To clarify:
$http = 'variable';
function http() {
return 'function';
}
var_dump($http);
var_dump(http());
Website URL including Protocol
You are attempting to get the value of http() via $http. Try this:
Website URL including Protocol
$http in only defined in a scope of http() function.
The function will trigger E_NOTICE errors as is, try this:
function http() {
return (getenv('HTTPS') == "on" ? 'https://' : 'http://');
}
Then as mkjasinski said,
Website URL including Protocol
I was trying to use this code:
if($_SERVER['HTTP_PORT'] !== 443 && !isset($_SERVER['HTTPS'])) {
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}
to make my login page and register page contain https to use the ssl to make sure that everything is secure, but when I run the page an error message shows up Undefined index error using $_SERVER['HTTPS']
so I decided to use another one:
if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
$pageURL .= "s";
}
but it didn't work. The https didn't show up and http is only there...
any idea how to do that with php...
Thanks
The solution is:
if($_SERVER["HTTPS"] != "on")
{
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
exit();
}
From the manual:
$_SERVER['HTTPS']
Set to a non-empty value if the script was queried through the HTTPS protocol.
Therefore, you should be using the following condition to check for an HTTPS connection:
if(!empty($_SERVER['HTTPS']))
{
// https://...
}
You'd better use $_SERVER['SERVER_PORT'].
HTTP_PORT doesnt always return the portnumber.
I have a template I made that sets variables that rarely change, call my headers, calls my banner and sidebar, loads a variable which shows the individual pages, then calls the footer. In one of my headers, I want the URL of the page in the user's address bar. Is there a way to do this?
Currently:
<?php
$title = "MySite - Contacts";
include("header.php");
.
.
.
?>
The main variables you'll be intersted in is:
$_SERVER['REQUEST_URI'] Holds the path visited, e.g. /foo/bar
$_SERVER['PHP_SELF'] is the path to the main PHP file (NOT the file you are in as that could be an include but the actual base file)
There are a ton of other useful variables worth remembering in $_SERVER, so either just:
print_r($_SERVER);
or just visit the doc at http://php.net/manual/en/reserved.variables.server.php
the Web address of the Page being called, can be obtained from the following function ::
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
I have been using this in many places, found on google.
It sounds like $_SERVER['REQUEST_URI'] is what you're after.