PHP Get A Dynamic Website

I am trying to get siteurl without hardcoding and store it in a constant inside the configuration file so that I can use it in my scripts regardless of subfolders / subfolders. This configuration file is stored in a folder in the main installation directory of the website.

Example:

Install the website at www.mydomain.com

Configuration file located at www.mydomain.com/my_folder/configuration.php

You want to declare a constant in the configuration.php file so that it always dynamically sets the path without requiring hard coding. So define("MY_SITE_URL", ???? )

What should be in place ???? in the code above to make it work, even if my main site is installed inside subfolders?

Examples. All pages will contain the same .php configuration file at the beginning of the file that is required

Set site on site 1:

The root directory of the website: www.mydomain.com/demos/site1/

Configuration Location: www.mydomain.com/demos/site1/my_folder/configuration.php

Subfolder: www.mydomain.com/demos/site1/1/index.php

Repeating MY_SITE_URL in index.php, you should specify www.mydomain.com/demos/site1/

Swap Folder: www.mydomain.com/demos/site1/1/2/index.php

Configuration Location: www.mydomain.com/demos/site1/my_folder/configuration.php

Repeating MY_SITE_URL in index.php, you should specify www.mydomain.com/demos/site1/

Set site on site 2:

Root directory of the website: www.mydomain.com/demos/site2/

Configuration Location: www.mydomain.com/demos/site2/my_folder/configuration.php

Subfolder: www.mydomain.com/demos/site2/1/index.php

the echo MY_SITE_URL in index.php should point to www.mydomain.com/demos/site2/

Subfolder: www.mydomain.com/demos/site2/1/2/index.php

Configuration Location: www.mydomain.com/demos/site2/my_folder/configuration.php

the echo MY_SITE_URL in index.php should point to www.mydomain.com/demos/site2/

Please note that TLDs are subject to change. This means setting up a website at www.mydomain.tk, www.mydomai.org, www.mydomain.net, www.mydomain.co.ca, etc. Should not make the code useless.

WHAT I AM FURTHER:

I tried to build a script. I almost got there, but not 100%. Here he is:

 function domain_url() { $pageURL = 'http'; if(isset($_SERVER["HTTPS"])) if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } //Remove any GET params from url $remmove_get = strtok($pageURL, '?'); //Removes trailing slash only at the end of string. This slash is received only when accessing a page within sub folders. $pageURL = preg_replace('{/$}', '', $remmove_get); //Remove page name $pageURL = dirname($pageURL); return $pageURL; } define("MY_SITE_URL", domain_url().'/'); 

The problem with my current code is:

Running the code on www.mydomain.com/demos/site1/ and www.mydomain.com/demos/site1/1/ gives MY_SITE_URL as www.mydomain.com/demos/site1/, and rightly so.

But when I run the code at www.mydomain.com/demos/site1/1/2/index.php, the echo MY_SITE_URL gives me www.mydomain.com/demos/site1/1/ when I really wanted: www.mydomain.com / demos / site1 /

How to solve this?

+4
source share
2 answers

If you always need a second directory, you can use

 $boom = explode("/", $_SERVER['REQUEST_URI']); $domain_url = $_SERVER["SERVER_NAME"].'/'.$boom[1].'/'.$boom[2]; echo $domain_url; 

If the site is never in the second directory, you can use: (this code should be a little more concise). Just replace site1 with the site name

 $site_search = "site1"; $url = $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']; $domain_url = substr($url, 0, strpos($url, $site_search)+strlen($site_search));; echo $domain_url; 

Update This will get the url for everything before / my _folder

 $domain_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $domain_url = substr($url, 0, strpos($url, '/my_folder')); define("MY_SITE_URL", domain_url().'/'); 

On a side note, you should try Code-Igniter . The structure makes this thing extremely light.

0
source

Try the following:

 define('PROTOCOL', array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://'); define('DOMAIN', $_SERVER['HTTP_HOST']); define('FOLDER', (dirname($_SERVER['SCRIPT_NAME']) == '/' ? null : dirname($_SERVER['SCRIPT_NAME'])) . '/'); define('BASE_URL', PROTOCOL . DOMAIN . dirname(FOLDER)); 

These 4 constants should cover what you are looking for. I wrote about this a little more here in this post - http://bit.ly/MPls0E

0
source

All Articles