Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9303431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:29:01+00:00 2026-06-18T23:29:01+00:00

I want to print out the current URL path, but my code doesn’t work

  • 0

I want to print out the current URL path, but my code doesn’t work propperly.

I use this in my file.php

echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];

When i open the url http://sub.mydomain.com/file.php it seems to work fine, and it prints "http://sub.mydomain.com/file.php"

But if i remove the .php extension so the url will be http://sub.mydomain.com/file instead, it prints "http://sub.mydomain.com/sub/file.php" which is wrong.

It prints the subdomain twice, and I don’t know why?

In my .htaccess file, I have a rewrite that makes it possible to removes .php extensions.

Anyone who can/want to help me please? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:29:02+00:00Added an answer on June 18, 2026 at 11:29 pm

    You need $_SERVER['REQUEST_URI'] instead of $_SERVER['SCRIPT_NAME'], cos $_SERVER['SCRIPT_NAME'] will always give you the file which is working at the moment.

    From manual:

    SCRIPT_NAME: Contains the current script’s path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. .

    I suppose this helps you getting current URL fully.

    echo 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    

    Notice: DO NOT RELY ON CLIENT’S HTTP_HOST, USE SERVER_NAME INSTEAD! SEE: What is the difference between HTTP_HOST and SERVER_NAME in PHP?

    Security Warning

    You need to filter (sanitize) $_SERVER['REQUEST_URI'] if you use it in anywhere (to print or store in database), cos it’s not safe.

    // ie: this could be harmfull
    /user?id=123%00%27<script...
    

    Hence, always filter user inputs before using them. At least use htmlspecialchars, htmlentities, strip_tags etc..

    Or something like this;

    function get_current_url($strip = true) {
        static $filter, $scheme, $host, $port; 
        if ($filter == null) {
            $filter = function($input) use($strip) {
                $input = trim($input);
                if ($input == '/') {
                    return $input;
                }
    
                // add more chars if needed
                $input = str_ireplace(["\0", '%00', "\x0a", '%0a', "\x1a", '%1a'], '',
                    rawurldecode($input));
    
                // remove markup stuff
                if ($strip) {
                    $input = strip_tags($input);
                }
    
                // or any encoding you use instead of utf-8
                $input = htmlspecialchars($input, ENT_QUOTES, 'utf-8');
    
                return $input;
            };
    
            $scheme = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME']
                : ('http'. (($_SERVER['SERVER_PORT'] == '443') ? 's' : ''));
            $host = $_SERVER['SERVER_NAME'];
            $port = ($_SERVER['SERVER_PORT'] != '80' && $scheme != 'https')
                ? (':'. $_SERVER['SERVER_PORT']) : '';
            }
        }
    
        return sprintf('%s://%s%s%s', $scheme, $host, $port, $filter($_SERVER['REQUEST_URI']));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is not pretty, but I want to print out the last generated
Ok, I want to make my program print out the date: 1/1/2009 But this
I want to get print out invoice but also wanna set margins and page
For debug purpose, I want to print out current executing function name in Java.
I want to create an alias that will print out the current working directory
I want to print out some debug messages in my erb template when it
I want to print out a derived class using the operator<< . When I
Say if i want to print out my integer stored inside a array ,
I'm doing some random experimentation and want to print out the address the program
I have a datagridview to show my entries and i want a print out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.