Hướng dẫn how can i tell if https is enabled in php? - làm cách nào để biết https đã được bật trong php hay chưa?

Tôi có dịp tiến thêm một bước và xác định xem trang web tôi đang kết nối có khả năng SSL hay không (một dự án yêu cầu người dùng cho URL của họ và chúng tôi cần xác minh rằng họ đã cài đặt gói API của chúng tôi trên trang web HTTP hoặc HTTPS).

Đây là chức năng tôi sử dụng - về cơ bản, chỉ cần gọi URL qua Curl để xem HTTPS có hoạt động không!

function hasSSL($url) 
{
    // take the URL down to the domain name
    $domain = parse_url($url, PHP_URL_HOST);
    $ch = curl_init('https://' . $domain);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); //its a  HEAD
    curl_setopt($ch, CURLOPT_NOBODY, true);          // no body
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  // in case of redirects
    curl_setopt($ch, CURLOPT_VERBOSE, 0); //turn on if debugging
    curl_setopt($ch, CURLOPT_HEADER, 1);     //head only wanted
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);    // we dont want to wait forever
    curl_exec($ch);
    $header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($header === 200) {
        return true;
    }
    return false;
}

Đây là cách đáng tin cậy nhất mà tôi đã tìm thấy không chỉ tìm hiểu xem bạn đang sử dụng HTTPS (như câu hỏi đặt ra), mà nếu bạn có thể (hoặc thậm chí nên) sử dụng HTTPS.

Lưu ý: Có thể (mặc dù không thực sự có khả năng ...) rằng một trang web có thể có các trang HTTP và HTTPS khác nhau (vì vậy nếu bạn được yêu cầu sử dụng HTTP, có lẽ bạn không cần phải thay đổi ..) Phần lớn các trang web là như nhau, và có lẽ nên định tuyến lại bạn, nhưng kiểm tra bổ sung này có việc sử dụng (chắc chắn như tôi đã nói, trong dự án mà người dùng nhập thông tin trang web của họ và bạn muốn đảm bảo từ phía máy chủ)

Mục đích của bài viết này là để kiểm tra xem trang này có được gọi từ ‘https, hay‘ http, chúng ta có thể sử dụng hai cách tiếp cận sau.

Cách tiếp cận 1: Kiểm tra xem kết nối có sử dụng SSL không và nếu giá trị của $ _Server [‘https,] được đặt, thì chúng ta có thể nói rằng kết nối được bảo mật và gọi từ‘ https. Nếu giá trị trống, điều này có nghĩa là giá trị được đặt thành ‘0, hoặc‘ tắt thì chúng ta có thể nói rằng kết nối không được bảo mật và trang được gọi từ ‘http. Check if the connection is using SSL and if the value of $_SERVER[‘HTTPS’] is set, then we can say that the connection is secured and called from ‘HTTPS’. If the value is empty, this means the value is set to ‘0’ or ‘off’ then we can say that the connection is not secured and the page is called from ‘HTTP’.

$ _Server là một mảng chứa tất cả thông tin về các tiêu đề, đường dẫn và vị trí tập lệnh yêu cầu. Nó sẽ có giá trị ‘không trống nếu yêu cầu được gửi qua HTTPS và trống hoặc‘ 0, nếu yêu cầu được gửi qua HTTP. is an array which contain all the information about request headers, paths, and script locations. It will have a ‘non-empty’ value if the request was sent through HTTPS and empty or ‘0’ if the request was sent through HTTP.

Syntax:

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}

Flowchart:

Hướng dẫn how can i tell if https is enabled in php? - làm cách nào để biết https đã được bật trong php hay chưa?

Lưu đồ 1

Example:

PHP

  if (isset(

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
0
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
1
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
2
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
3

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
5
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
6
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
7

  

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
9

  

Warning : Connection is not secured,Page is called from HTTP
1

  

Warning : Connection is not secured,Page is called from HTTP
3

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
5
Warning : Connection is not secured,Page is called from HTTP
6
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
7

  

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
9

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
0

Output:

Warning : Connection is not secured,Page is called from HTTP

Cách tiếp cận 2: Một vấn đề với cách tiếp cận trước đó là trong một số máy chủ, $ _server [‘https,] không được xác định và điều này có thể dẫn đến một thông báo lỗi trong khi kiểm tra xem trang đó có được gọi từ‘ https hay từ ‘http. Vì vậy, để khắc phục điều này, chúng tôi cũng phải kiểm tra số cổng máy chủ, nếu số cổng được sử dụng là 443, kết nối được thực hiện thông qua ‘https. A Problem with the earlier approach is that in some servers, the $_SERVER[‘HTTPS’] is undefined and this could lead to an error message while checking that the page is called from ‘HTTPS’ or from ‘HTTP’. So to overcome this, we have to check for server port number also, if the port number used is 443, the connection is made through ‘HTTPS’.

Pseudocode:

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request

Syntax:

if ((isset($_SERVER['HTTPS']) && 
        (($_SERVER['HTTPS'] == 'on'))) 
 || (isset($_SERVER['HTTPS']) && 
           $_SERVER['SERVER_PORT'] == 443))
  {
   Page is called through HTTPS 
 }
 else
  {
   Page is called through HTTPS
  }

Flowchart:

Hướng dẫn how can i tell if https is enabled in php? - làm cách nào để biết https đã được bật trong php hay chưa?

Lưu đồ 2

Example:

PHP

  if (isset(

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
0
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
1
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
2
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
3

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
2
Warning : Connection is not secured,Page is called from HTTP
3

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
5
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
6
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
7

if ((isset($_SERVER['HTTPS']) && 
        (($_SERVER['HTTPS'] == 'on'))) 
 || (isset($_SERVER['HTTPS']) && 
           $_SERVER['SERVER_PORT'] == 443))
  {
   Page is called through HTTPS 
 }
 else
  {
   Page is called through HTTPS
  }
1
Connection is not secured and page is called from HTTP
1

if ((isset($_SERVER['HTTPS']) && 
        (($_SERVER['HTTPS'] == 'on'))) 
 || (isset($_SERVER['HTTPS']) && 
           $_SERVER['SERVER_PORT'] == 443))
  {
   Page is called through HTTPS 
 }
 else
  {
   Page is called through HTTPS
  }
1
Connection is not secured and page is called from HTTP
3

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
2
Connection is not secured and page is called from HTTP
3

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
5
Warning : Connection is not secured,Page is called from HTTP
6
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
7

Warning : Connection is not secured,Page is called from HTTP
3

Cách tiếp cận 2: Một vấn đề với cách tiếp cận trước đó là trong một số máy chủ, $ _server [‘https,] không được xác định và điều này có thể dẫn đến một thông báo lỗi trong khi kiểm tra xem trang đó có được gọi từ‘ https hay từ ‘http. Vì vậy, để khắc phục điều này, chúng tôi cũng phải kiểm tra số cổng máy chủ, nếu số cổng được sử dụng là 443, kết nối được thực hiện thông qua ‘https.

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
Warning : Connection is not secured,Page is called from HTTP
3

Lưu đồ 2

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
9

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
9

  

Warning : Connection is not secured,Page is called from HTTP

  

Warning : Connection is not secured,Page is called from HTTP
3

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
2if (isset(
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
0
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
1
if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
2
check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
8

  

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
9

check if $_SERVER['HTTPS'] is set and $_SERVER['HTTPS']
is set to 'on' then Connection is Secured and through 
HTTPS request

OR 

if  $_SERVER['HTTPS'] is set and $_SERVER['SERVER_PORT'] 
is 443 ( https use 443 ) then Connection is secured 

else
    
Connection is not secured and made through HTTP request
0

Output:

Connection is not secured and page is called from HTTP

Làm thế nào để tôi biết nếu PHP là HTTPS?

Cách tiếp cận 1: Kiểm tra xem kết nối có sử dụng SSL không và nếu giá trị của $ _Server ['https'] được đặt, thì chúng ta có thể nói rằng kết nối được bảo mật và gọi từ 'https'. Nếu giá trị trống, điều này có nghĩa là giá trị được đặt thành '0' hoặc 'tắt' thì chúng ta có thể nói rằng kết nối không được bảo mật và trang được gọi từ 'http'.Check if the connection is using SSL and if the value of $_SERVER['HTTPS'] is set, then we can say that the connection is secured and called from 'HTTPS'. If the value is empty, this means the value is set to '0' or 'off' then we can say that the connection is not secured and the page is called from 'HTTP'.

Làm thế nào tôi có thể biết nếu URL là HTTPS hay HTTP trong PHP?

Làm cách nào để kiểm tra xem một chuỗi là https? Để kiểm tra xem Chuỗi có bắt đầu bằng cách sử dụng HTTP hay không, hãy sử dụng chức năng tích hợp PHP (). strpos () lấy chuỗi và chuỗi con làm đối số và trả về 0, nếu chuỗi bắt đầu bằng cách http, thì không. Loại kiểm tra này hữu ích khi bạn muốn xác minh xem chuỗi đã cho có phải là URL hay không.use PHP built-in function strpos(). strpos() takes the string and substring as arguments and returns 0, if the string starts with “http”, else not. This kind of check is useful when you want to verify if given string is an URL or not.

Làm thế nào để tôi biết nếu một trang web là HTTPS hay không?

May mắn thay, có hai kiểm tra nhanh để giúp bạn chắc chắn: Nhìn vào Bộ định vị tài nguyên thống nhất (URL) của trang web.Một URL an toàn nên bắt đầu với các https, chứ không phải là http http.Các S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S SLook at the uniform resource locator (URL) of the website. A secure URL should begin with “https” rather than “http.” The “s” in “https” stands for secure, which indicates that the site is using a Secure Sockets Layer (SSL) Certificate.