Hướng dẫn how can i tell if url is https or http in php? - làm cách nào để biết url là https hay http trong php?

bạn có thể sử dụng parse_url

<?php
$url = parse_url('https://example.org');

if($url['scheme'] == 'https'){
   // is https;
}
?>

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 8:42Sep 19, 2011 at 8:42

Hướng dẫn how can i tell if url is https or http in php? - làm cách nào để biết url là https hay http trong php?

Mihai Iorgamihai IorgaMihai Iorga

38.7K15 Huy hiệu vàng107 Huy hiệu bạc106 Huy hiệu đồng15 gold badges107 silver badges106 bronze badges

1

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";

Hướng dẫn how can i tell if url is https or http in php? - làm cách nào để biết url là https hay http trong php?

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 8:45Sep 19, 2011 at 8:45

Tasmaniskitasmaniskitasmaniski

4.7073 Huy hiệu vàng33 Huy hiệu bạc64 Huy hiệu đồng3 gold badges33 silver badges64 bronze badges

Có lẽ điều này có thể giúp

$_SERVER['SERVER_PROTOCOL'];

Đã trả lời ngày 19 tháng 9 năm 2011 lúc 8:45Sep 19, 2011 at 8:45

TasmaniskitasmaniskiElitmiar

4.7073 Huy hiệu vàng33 Huy hiệu bạc64 Huy hiệu đồng72 gold badges179 silver badges228 bronze badges

2

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 url is https or http in php? - làm cách nào để biết url là https hay http trong php?

Lưu đồ 1

Example:

PHP

<?php

  ____10

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
1
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
2
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
3
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
4
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
5

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
7
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
8
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
9

  

$_SERVER['SERVER_PROTOCOL'];
1

  

$_SERVER['SERVER_PROTOCOL'];
3

  

$_SERVER['SERVER_PROTOCOL'];
5

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
7
$_SERVER['SERVER_PROTOCOL'];
8
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
9

  

$_SERVER['SERVER_PROTOCOL'];
1

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

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 url is https or http in php? - làm cách nào để biết url là https hay http trong php?

Lưu đồ 2

Example:

PHP

<?php

  ____10

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
1
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
2
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
3
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
4
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
5

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

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
7
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
8
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
9

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
3

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
5

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
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
5

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
7
$_SERVER['SERVER_PROTOCOL'];
8
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
9

$_SERVER['SERVER_PROTOCOL'];
5

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 (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
$_SERVER['SERVER_PROTOCOL'];
5

Lưu đồ 2

if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
6
$_SERVER['SERVER_PROTOCOL'];
1

$_SERVER['SERVER_PROTOCOL'];
1

  

$_SERVER['SERVER_PROTOCOL'];

  

$_SERVER['SERVER_PROTOCOL'];
5

if (isset($_SERVER['HTTPS']))
{
 // page is called from https
 // Connection is secured
}
else
{
 // page is called from http
 // Connection is not secured
}
4
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
0
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
1
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
2
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
3
if (substr($string, 0, 7) == "http://")
    $res = "http";

if (substr($string, 0, 8) == "https://")
    $res = "https";
4
Warning : Connection is not secured,Page is called from HTTP
0

  

$_SERVER['SERVER_PROTOCOL'];
1

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

Output:

Connection is not secured and page is called from HTTP

Làm thế nào yêu cầu kiểm tra là http hoặc https trong PHP?

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 biết nếu url của tôi là http hoặc https?

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 SA 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.

Làm thế nào tôi có thể biết nếu HTTPS được bật trong PHP?

Trong PHP, cách phổ biến nhất để xác định xem một yêu cầu được thực hiện thông qua giao thức HTTPS hay không có sử dụng thuộc tính HTTPS trên SuperGlobal $ _Server như vậy: $ _Server ['https'];$_SERVER['HTTPS'];

Làm cách nào để kiểm tra xem một chuỗi là http?

Để 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.