0

So I am making a website on my localhost, the folder name is "veco" therefore my URL link is http://localhost/veco/

Im currently using this code to get the home url "http://localhost/veco/":

<?php 
function home_url() {
    // output: /myproject/index.php
    $currentPath = $_SERVER['PHP_SELF']; 

    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
    $pathInfo = pathinfo($currentPath); 

    // output: localhost
    $hostName = $_SERVER['HTTP_HOST']; 

    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

    // return: http://localhost/myproject/
    return $protocol.$hostName.$pathInfo['dirname']."/";
}
?>

Now I'm on page "about", my URL now is http://localhost/veco/about Now on the about page, I made a form

<form action="data/cs_menu.php" method="POST">
    <input type="text" name="f_menu">
    <input type="submit" name="save_menu">
</form>

Now when I submit this form, it will redirect me to http://localhost/veco/about/data/cs_menu.php which is correct, but when I use the function home_url, it gives me "http://localhost/veco/data/", not "http://localhost/veco/"

Any ideas? I am not using Wordpress right now, but If you are familiar with WordPress, they have a function "home_url();" which returns the "http://localhost/FILENAME/" which is I am trying to copy.

Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31
ICG DEVS
  • 195
  • 3
  • 15
  • `home_url( 'data/cs_menu.php' )` – Sally CJ Aug 29 '18 at 08:25
  • it still return "http://localhost/CodeSensei/data/", what I want is "http://localhost/CodeSensei" – ICG DEVS Aug 29 '18 at 08:33
  • Then `home_url( 'cs_menu.php' )` ? – Sally CJ Aug 29 '18 at 08:39
  • same, I only want to print the home url which is "http://localhost/veco/", but it gives me "http://localhost/veco/data" instead – ICG DEVS Aug 29 '18 at 08:51
  • Wait. WordPress has a function named `home_url()`. Did you or how could you overwrite it? Where did you put the `home_url()` code you showed in the question? – Sally CJ Aug 29 '18 at 09:03
  • No, sorry, I am not using Wordpress or any CMS, What I mean is I am trying to imitate wordpress function, as you know, wordpress has a function of home_url() which will return homepage url for example "http://localhost/home", "http://facebook.com", "https://stackoverflow.com" . Now I am making my own CMS and I want to add a home_url(); function too. But my function above is not working on other pages. – ICG DEVS Aug 29 '18 at 09:15
  • You have flagged your question as a WordPress problem, so it wasn't very clear. Now, as we all know that you are not using WordPress, it is much clearer. The easiest way is to define constant with your home url - define('HOME_URL', 'http://localhost/veco/"); - and now you may use it as easy as echo HOME_URL; – Łukasz Aug 29 '18 at 09:31
  • 1
    I see @ICGDEVS. But in WordPress, `home_url()` is based on a static value of the home page URL — e.g. `http://localhost/wp` (saved in the database, or could also be set in or overriden by a constant named `WP_HOME`). And that the function accepts an *optional* parameter — `$path`. If `$path` is specified, you'd get `'http://localhost/wp' . $path`. Otherwise, you'd always get `http://localhost/wp` — the base URL. – Sally CJ Aug 29 '18 at 09:34
  • @Łukasz, sorry, I compared my function to wordpress thats why I flagged it with wordpress, but I already removed it to avoid confusion, thanks. Now going back to your answer which is `define('HOME_URL', 'localhost/veco/");` then this means this is static, if my filename "veco" will be rename then I will also need to change the code? Is there any way to make it dynamic `"localhost/veco/"`? – ICG DEVS Aug 29 '18 at 09:47
  • @SallyCJ I see, thanks, so my function is wrong? Any idea how can I print `http://localhost/veco/` ? Should I just removed `$_SERVER['PHP_SELF']` ? – ICG DEVS Aug 29 '18 at 09:51
  • @ICGDEVS it's absolutely normal to setup new localization, we do it in WordPress, you may do it, too :) Of course you may make it dynamic by reading $_SERVER variable but there are many servers, many configurations and, in my opinion, it's not worth it. And I tried to do exactly what you want, many years ago, but there were nobody to tell me - stop it, define constant, forget it :) – Łukasz Aug 29 '18 at 09:53
  • Okay. Thanks Łukasz and SallyCJ, So what I am trying to achieve is impossible unless if it is a static. Thank you very much. :) – ICG DEVS Aug 29 '18 at 09:57
  • Hello guys, I found an answer, I only added two lines of codes to my function, you can check it below the answers. – ICG DEVS Aug 29 '18 at 15:21
  • @ICGDEVS Please ignore my comment on the `$_SERVER['PHP_SELF']` thing - to avoid confusion. ;) Cheers! – Sally CJ Aug 30 '18 at 02:00

3 Answers3

0

I am not using Wordpress right now, but If you are familiar to wordpress, they have function "home_url();"

WordPress looks up the value from a database, where it is set when you install WordPress.

There is, generally, no way to determine the URL of the homepage from an arbitrary page on the site. The closest you could get would be to manually add a rule to every page along the lines of "The homepage is exactly two directories above this page".

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can try this :

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

And, if you plan using https, you can use this function :

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

And call it this way :

echo url();
#=> http://127.0.0.1/foo

As for the $_SERVER global variable the docs can be found here

The original answer has been posted on this thread.

You should check it and let me know if this solves your problem. Hope this helps.

iceberg53
  • 330
  • 1
  • 7
0

Guys I finally found the solution, it is also dynamic. I only edited my function and added this:

   $home_url = (explode("/",$full_url));
   return $home_url[0]."//".$home_url[2]."/".$home_url[3];

Now Here's the full final code:

function home_url() 
{
    // output: /myproject/index.php
    $currentPath = $_SERVER['PHP_SELF']; 

    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
    $pathInfo = pathinfo($currentPath); 

    // output: localhost
    $hostName = $_SERVER['HTTP_HOST']; 

    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

    // return: http://localhost/myproject/
    $full_url = $protocol.$hostName.$pathInfo['dirname']."/";
    $home_url = (explode("/",$full_url));
    return $home_url[0]."//".$home_url[2]."/".$home_url[3];

}

There might be some bugs to it, but for now I didn't see any. If you can point some then please tell me. Thank you

ICG DEVS
  • 195
  • 3
  • 15
  • Just be careful, since the code may not work in all cases or at all times - if the home URL is always in this format: `{protocol}://{domain}/{sub-folder in root}` (e.g. `http://localhost/veco`), the code would work; otherwise, it would fail. Just my 1 cent. :) Anyway, don't forget to accept your answer - it'd be helpful to others. ;) – Sally CJ Aug 30 '18 at 01:53
  • 1
    Okay @SallyCJ. Thank you very much – ICG DEVS Aug 30 '18 at 14:50