...I would like the page log...">

How to redirect a page to https in php?

I have a login form:

<form method =POST action="/login.php">
...
</form>

I would like the page login.phpredirected to use https.

I do not want to send the user to https://.../login.php, because they can change the link. but I want to do server-side redirects before parsing the login form data and registering the user.

I found an example:

if($_SERVER["HTTPS"] != "on") {
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
   exit();
}

but i don't $_SERVER["HTTPS"]have if ivar_dump($_SERVER);

I have $_SERVER['SERVER_PORT']witch is 80.

any ideas?

thank

+5
source share
4 answers

/login.php HTTP HTTPS, HTTPS, ​​ .

, URL-, , , HTTPS.

, HTTPS, :

if (!isset($_SERVER['HTTPS']) || !$_SERVER['HTTPS']) {
    // request is not using SSL, redirect to https, or fail
}

443, , , PHP $_SERVER['HTTPS'] , SSL, .

EDIT:

, https http , , , HTTP, , https, , .

+6

, , https:// , , ​​ https://. .

mod_rewrite, http:// https://, , .

+5
if($requireSSL && $_SERVER['SERVER_PORT'] != 443) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit();
}
+5

, index.php, HTTP HTTPS index.php. , , ​​/login.php HTTPS, HTTP.

login.php , login.php HTTPS-URL, , , .

, $_SERVER['SERVER_PORT'] 80 $_SERVER["HTTPS"] , login.php, , HTTP , , . , , .

, PHP . mod_rewrite Apache HTTPD.

, , URL-, http://example.com/foo/:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^foo/$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
+3

All Articles