Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?

Bạn có thể làm điều đó như thế này, nhưng xin lỗi tiếng Anh của tôi không đủ tốt.

Show

Đầu tiên, lấy URL cơ sở tại nhà với mã đơn giản này ..

Tôi đã kiểm tra mã này trên máy chủ cục bộ và công khai của mình và kết quả là tốt.



// and test it

echo home_base_url();

đầu ra sẽ thích điều này:

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/

Sử dụng chức năng

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
1 tại
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
2 của trang web của bạn và xác định nó

Và sau đó bạn có thể sử dụng chức năng này để tải tập lệnh, CSS và nội dung qua URL thích

'."\n";

?>

sẽ tạo ra đầu ra như thế này:


Và nếu kịch bản này hoạt động tốt ,,!

https://ide.geeksforgeeks.org/
4
https://ide.geeksforgeeks.org/
5
'."\n";

?>
5
https://ide.geeksforgeeks.org/
7

0
https://ide.geeksforgeeks.org/
9

6
https://ide.geeksforgeeks.org/
1$_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

Đầu ra của mã trên là https://ide.geeksforgeek.org/ thay vì https://ide.geeksforgeek.org/index.php. Để khắc phục sự cố này, cần phải thay thế, $ _Server [‘request_uri,] bằng $ _server [‘ php_elf].

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?

Chương trình 3: Ví dụ này đang hiển thị URL tệp PHP hiện đang thực hiện.

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
00
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
6
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
4
'."\n";

?>
5____112

Đầu ra của mã trên là https://ide.geeksforgeek.org/ thay vì https://ide.geeksforgeek.org/index.php. Để khắc phục sự cố này, cần phải thay thế, $ _Server [‘request_uri,] bằng $ _server [‘ php_elf].

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?

Chương trình 3: Ví dụ này đang hiển thị URL tệp PHP hiện đang thực hiện.

Đầu ra của mã trên là https://ide.geeksforgeek.org/ thay vì https://ide.geeksforgeek.org/index.php. Để khắc phục sự cố này, cần phải thay thế, $ _Server [‘request_uri,] bằng $ _server [‘ php_elf].

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?



Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
Chương trình 3: Ví dụ này đang hiển thị URL tệp PHP hiện đang thực hiện.


local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 00local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 5 local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 6local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 7local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 8local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 9'."\n"; ?> 0local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 7local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 8local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 9'."\n"; ?> 4'."\n"; ?> 5____112

  • local machine : http://localhost/my_website/ or https://myhost/my_website 
    
    public : http://www.my_website.com/ or https://www.my_website.com/
    
    13
    '."\n";
    
    ?>
    
    8
    '."\n";
    
    ?>
    
    9
    
    
    0
    
    
    1

local machine : http://localhost/my_website/ or https://myhost/my_website public : http://www.my_website.com/ or https://www.my_website.com/ 00 3 '."\n"; ?> 8 '."\n"; ?> 9 6 1

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?






local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
13
'."\n";

?>
8
'."\n";

?>
9

0

1

Cách tiếp cận: Có một vài bước để có được URL hoàn chỉnh của trang hiện đang chạy dưới đây:There are a few steps to get the complete URL of the currently running page which are given below:

  • Tạo một biến PHP sẽ lưu trữ URL ở định dạng chuỗi.
  • Kiểm tra xem HTTPS có được bật bởi máy chủ không. Nếu đúng như vậy, hãy nối lại HTTPS, vào chuỗi URL. Nếu HTTPS không được bật, hãy nối HTTP, vào chuỗi URL.
  • Nối các biểu tượng thông thường, tức là,: // vào URL.
  • Nối HTTP_HOST (máy chủ mà chúng tôi đã yêu cầu, ví dụ: www.google.com, www.yourdomain.com, v.v.) Tên của máy chủ.
  • Nối thêm request_uri (tài nguyên mà chúng tôi đã yêu cầu, ví dụ: /index.php, v.v.) vào chuỗi URL.

Lưu ý: Sử dụng hàm isset () để kiểm tra xem HTTPS có được bật hay không. Hàm isset () được sử dụng để kiểm tra một biến có tồn tại hay không. Ví dụ 1: Ví dụ này minh họa việc nhận URL của trang hiện tại. Use isset() function to check whether HTTPS is enabled or not. The isset() function is used to check a variable exists or not.
Example 1: This example illustrates getting the url of the current page.

PHP

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
3

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
6
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
4
'."\n";

?>
5
'."\n";

?>
6

'."\n";

?>
7
'."\n";

?>
8
'."\n";

?>
9

0

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4

3
'."\n";

?>
8
'."\n";

?>
9

6

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
5
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
'."\n";

?>
8

1

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

Output:

https://ide.geeksforgeeks.org/

Ví dụ 2: Lấy URL của trang web bằng $ _Server [‘http_host,] sẽ trả về tiêu đề máy chủ từ yêu cầu hiện tại. Getting the url of the webpage using $_SERVER[‘HTTP_HOST’] that will return the host header from the current request.

PHP

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
3

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
6
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
4
'."\n";

?>
5
'."\n";

?>
6

'."\n";

?>
7
'."\n";

?>
8
'."\n";

?>
9

0

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4

3
'."\n";

?>
8
'."\n";

?>
9

6

1

https://ide.geeksforgeeks.org/
4
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
5
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
'."\n";

?>
8

1

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

Output:

https://ide.geeksforgeeks.org/

Ví dụ 2: Lấy URL của trang web bằng $ _Server [‘http_host,] sẽ trả về tiêu đề máy chủ từ yêu cầu hiện tại.

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
https://ide.geeksforgeeks.org/
3
This example is displaying the currently executing PHP file URL.

PHP

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
3

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
6
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
4
'."\n";

?>
5
'."\n";

?>
6

'."\n";

?>
7
'."\n";

?>
8
'."\n";

?>
9

0

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4

3
'."\n";

?>
8
'."\n";

?>
9

6

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
'."\n";

?>
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
5
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
'."\n";

?>
8

1

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

Output:

https://ide.geeksforgeeks.org/index.php

Ví dụ 2: Lấy URL của trang web bằng $ _Server [‘http_host,] sẽ trả về tiêu đề máy chủ từ yêu cầu hiện tại.This example describes getting the complete url of the webpage.

PHP

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
3

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
5
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
6
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
0
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
9
'."\n";

?>
4
'."\n";

?>
5
'."\n";

?>
6

'."\n";

?>
7
'."\n";

?>
8
'."\n";

?>
9

0

1

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
60
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
74
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
7
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
8
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
41
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
9

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/
4
Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
8
'."\n";

?>
8

1

Hướng dẫn how to get website url in php? - cách lấy url trang web bằng php?
1

Output:

https://ide.geeksforgeeks.org/index.php

Ví dụ 2: Lấy URL của trang web bằng $ _Server [‘http_host,] sẽ trả về tiêu đề máy chủ từ yêu cầu hiện tại.


Làm thế nào tôi có thể nhận URL trong PHP?

Để có được URL trang hiện tại, PHP cung cấp một biến SuperGlobal $ _Server.$ _Server là một biến tích hợp của PHP, được sử dụng để có URL trang hiện tại.Nó là một biến Superglobal, có nghĩa là nó luôn có sẵn trong tất cả các phạm vi.PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

Địa chỉ web PHP là gì?

Định nghĩa của URL PHP.Nói chung, URL có nghĩa là người định vị tài nguyên thống nhất.Tương tự như vậy, URL trong ngôn ngữ lập trình PHP cũng giống nhau.URL không có gì ngoài một địa chỉ trang web.Nó giúp kết nối máy khách và máy chủ khi được duyệt bằng một liên kết cụ thể.Uniform Resource Locater. Likewise, URL in PHP Programming Language is also the same. URL is nothing but a website address. It helps in connecting the client and the server when browsed using a specific link.

BaseURL PHP là gì?

URL cơ sở được sử dụng để tạo các liên kết trang web nội bộ một cách động trong trang web.Bạn có thể nhận URL cơ sở từ chuỗi URL đầy đủ bằng PHP.Hàm parse_url () giúp phân tích các thành phần từ URL trong PHP.URL cơ sở có thể được lấy từ một chuỗi bằng hàm parse_url ().24-feb-2022.used to create internal web page links dynamically in the website. You can get the base URL from the full URL string using PHP. The parse_url() function helps to parse components from URL in PHP. The base URL can be retrieved from a string using PHP parse_url() function.24-Feb-2022.