Best way to get the current page url in php

In every website you will have unique address. When you are doing a project sometime we need to access that website for link up my Stylesheet, HTML5, Javascript, PHP etc files. And sometimes you need to dynamically generate the title of a page based on its URL. What will you do? If that page have the HTTP or HTTPS what will happen. How to get HTTP or HTTPS and the base link of website using PHP?

Here, you will see the path of code. But you need to give more effort for its security & for more easy way to make that functions. I am not going to tell you because of, A greatest person told that,
A teacher can only show the path & maybe give him/her tips of obstacle but you need to decide what to do? and how to do? face with that obstacle.
If you want to get current url path what will you do: Here's the code... 

PHP:


    $currentUrl = $_SERVER[SCRIPT_URI];

    echo $currentUrl; 


Get the domain name only with HTTP or HTTPS.

PHP:


    public function url()
    {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 
            'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $domainName = $_SERVER['HTTP_HOST'].'/';
        return $protocol.$domainName;
    }
    // Here I just declared define() for you can call that 'SITE_URL' 
    // from anywhere(after including that function file). 
    define( 'SITE_URL', url() );


Now it's you turn how to make it more short and more hard to break it's security. No one can attack on this script.


You have learned,
How to get current URL path?
How to get website URL or only domain?

If you like that, Please do comment and share.
And if you make that function for easy let me know. Let's see is your code bullet proof or not? 

Comments

Popular posts from this blog

Automatic Date and Time Enable Kali Linux

How to start hotspot in kali linux

How to detect proxy IP or real IP