Jump to content



Photo

Building A Dynamic Base Url


  • Please log in to reply
8 replies to this topic

#1 Wasabi

Wasabi

    Member

  • Members
  • PipPip
  • 25 posts
  • LocationCA

Posted 5 January 2012 - 3:13 PM

I have a constant building a dynamic base_url:

define('BASE_URL', "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);


The current base_url
"http://training:8888/php/PHP6:MySQL%205/16/user_registration/htdocs/test.php/index.php"


I've used:
	echo(BASE_URL . '/index.php' . "<br />"); // Define the URL:
	
	echo str_ireplace('htdocs/','', BASE_URL ); //Not the desired result

and arrived at:

"http://training:8888/php/PHP6:MySQL%205/16/user_registration/test.php"

I'm having issues, adapting the str_ireplace() function to remove all segments after
"htdocs" so another uri can be appended:

$url = BASE_URL . '/somepage.php'; // Define the URL:

"http://training:8888/php/PHP6:MySQL%205/16/user_registration/htdocs/somepage.php."//Desired result

Happy New Year!
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,908 posts
  • LocationState College, PA (USA)

Posted 6 January 2012 - 8:56 AM

I really don't understand what you're trying to do here. Also, do you really want a period at the end of the URL as in your desired result?
  • 0

#3 Wasabi

Wasabi

    Member

  • Members
  • PipPip
  • 25 posts
  • LocationCA

Posted 6 January 2012 - 11:40 AM

The "." at the end was not desired :)

I'm trying to trim the result of
"http://training:8888/php/PHP6:MySQL%205/16/user_registration/htdocs/test.php/index.php"

to just the requesting directory:
"http://training:8888/php/PHP6:MySQL%205/16/user_registration/htdocs" //desired base URL

Allowing me to append any document file:

$url = BASE_URL . '/somepage.php'; // Define the URL

Hope I clarified my request. Thanks for the reply and inspiration.

PS: I'm experimenting with http_build_url();

Happy New Year!
  • 0

#4 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,908 posts
  • LocationState College, PA (USA)

Posted 6 January 2012 - 12:29 PM

Okay, that makes more sense, but should it really be "http://training:8888....php/index.php" or should that be "http://training:8888...est/index.php". And if it's the latter, should that be trimmed down to "http://training:8888.../htdocs/test/"?
  • 0

#5 Wasabi

Wasabi

    Member

  • Members
  • PipPip
  • 25 posts
  • LocationCA

Posted 6 January 2012 - 1:09 PM

I'm attempting to trim away "test.php" so that I can append "somepage.php". Thus making the base_url usable in any config file. I've seen the same in frameworks, wanting to recreate it "old school."

I appreciate frameworks, but still like making apps "from scratch" when time permits, having been with your publications since PHP 4.

Thanks Again!
  • 0

#6 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,908 posts
  • LocationState College, PA (USA)

Posted 6 January 2012 - 1:56 PM

So, just to be absolutely clear here, the URL would include BOTH test.php and index.php and you want to strip both?
  • 0

#7 Wasabi

Wasabi

    Member

  • Members
  • PipPip
  • 25 posts
  • LocationCA

Posted 6 January 2012 - 3:47 PM

The $base_url should NOT include test.php, however when using:
 define('BASE_URL', "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

I get the full path to the requesting document file, I'm trying to strip in this case (test.php), so it can be replaced with

$url = BASE_URL . '/whateverpage.php';

Again, thanks for your patience.
  • 0

#8 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,908 posts
  • LocationState College, PA (USA)

Posted 6 January 2012 - 6:50 PM

I think the dirname() function will chop off the file name and return the rest.
  • 0

#9 Wasabi

Wasabi

    Member

  • Members
  • PipPip
  • 25 posts
  • LocationCA

Posted 6 January 2012 - 7:59 PM

I found these forum post with an almost identical query as mine:http://www.webdevelo...ad.php?t=165220 and http://blog.chapagai...in-or-base-url/.

If you could add insight to there solutions it would be appreciated. Your patience is appreciated.
  • 0