How to Get Current Page URL in PHP

PHP program to get complete URL of currently running pages

PHP Function 1:

// Program to display URL of current page. 
  
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
else
    $link = "http"; 
  
// Here append the common URL characters. 
$link .= "://"; 
  
// Append the host(domain name, ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 
  
// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 
      
// Print the link 
echo $link;

PHP function 2:

// Program to display current page URL. 
  
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 
                "https" : "http") . "://" . $_SERVER['HTTP_HOST'] .  
                $_SERVER['REQUEST_URI']; 
  
echo $link;

PHP function 2:

// Program to display complete URL     
      
if(isset($_SERVER['HTTPS']) &&  
            $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
    else
        $link = "http"; 
  
// Here append the common URL  
// characters. 
$link .= "://"; 
      
// Append the host(domain name, 
// ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 
      
// Append the requested resource 
// location to the URL 
$link .= $_SERVER['PHP_SELF']; 
      
// Display the link 
echo $link;

PHP funtion 4:

$uri = $_SERVER['REQUEST_URI'];
echo $uri; // Outputs: URI
 
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
 
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $url; // Outputs: Full URL
 
$query = $_SERVER['QUERY_STRING'];
echo $query; // Outputs: Query String

 

How to choose a random word from an array php
<?php $Words =array("Black","Yellow","Blue","Red","Purple","Pink", "White"); $rand_word=array_rand($Words,2); $Result =
How to minify HTML code of PHP page
<?php ob_start("minifier"); function minifier($code) { $search =
How to copy a file from one directory to another using PHP
<?php // Copy the file from /user/desktop/geek.txt
How to Deleting all files from a folder using PHP
<?php // PHP program to delete all
How do I test if an remote image file exists in php
if(@get_headers($your_url) == 'HTTP/1.1 404 Not Found') {
Human Readable File Size with PHP
function bytesToHuman($bytes) { $units = ['B', 'KB',
PHP Simple File Upload from Remote URL
<?php if($_POST){ //get the url $url =
PHP unzipper / extracts .zip archives and .gz files on webservers
<?php $timestart = microtime(TRUE); $arc = new
Get the first character in a string with PHP
//Example string. $string = "My milkshake brings
How to generate a random token with PHP
//Generate a random string. $token = openssl_random_pseudo_bytes(16);
How to handle HTML checkboxes with PHP
<?php //Set "terms accepted" to false by
Generating random names with PHP
<?php //PHP array containing forenames. $names =
How to create PHP Timer Countdown
//You must call the function session_start() before
PHP – Do not echo out HTML!
Wrong: <?php echo '<div id="user_profile">'; echo '<h1>'
How to rotate a series of words on page load using PHP
$words = array('fox', 'hippo', 'dog', 'cat'); echo
How to check if a value exists in an array in PHP
$zoo = array("Lion", "Elephant", "Tiger", "Zebra", "Rhino",
How to Get the Current Date and Time in PHP
date_default_timezone_set('Asia/Kolkata'); $date = date('d-m-y h:i:s'); echo $date;
How to remove HTML special characters from a string in PHP
$my_str = "String with <b>special</b> characters."; //
How to extract substring from a string in PHP
$str = "Hello World!"; echo substr($str, 0,

Top