base url in php

Try this:

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Learn more about the $_SERVER predefined variable.

If you plan on using https, you can use this:

function url(){
  return sprintf(
    "%s://%s%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_NAME'],
    $_SERVER['REQUEST_URI']
  );
}

echo url();
#=> http://127.0.0.1/foo
Per this answer, please make sure to configure your Apache properly so you can safely depend on SERVER_NAME.

<VirtualHost *>
    ServerName example.com
    UseCanonicalName on
</VirtualHost>
NOTE: If you're depending on the HTTP_HOST key (which contains user input), you still have to make some cleanup, remove spaces, commas, carriage return, etc. Anything that is not a valid character for a domain. Check the PHP builtin parse_url function for an example.

4
6
GaetanoM 60 points

                                    &lt;?php echo &quot;http://&quot; . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?&gt;

4 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
base url php in javascript php set base url function get base url in php how create base url in php link base url in php get base url from url php php echo base_url() $base_url php get base name from url in php &lt;?php echo base_url();?&gt; PHP Request base url how to get base url of any function using php get the base url in php base link php how to get the base url in php change base_url php baseurl php php get url base syntax for getting base url in php base_url in php get base_url php get base url $this-&gt;pageUrl php how to get base url usin php php basename on url. echo base_url php set base url in php how can use base url use in php base64 url php get base url from string php base url in php include server base url php php get url basename define base url in php get base url from a url php &lt;?php echo base_url(); ?&gt; php use base url php base_url &lt;?php echo BASE_URL; ?&gt; base_url() php baseurl get base url php php how to get base url php create base url base_url() in php php find base url get base url in php &lt;?php get base url php get base uri base_url php define base url php create base url php how to set base url in php how to find base url in php php get base_url php get base url from $server base url syntax in php how to make base url in php base url php base url path in php php url basename how to get base url in php get the base url php php get base url php set the base url &lt;?php echo base_url() ?&gt; php get base url from string php base url base url in php
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source