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

  • Home
  • SEARCH
  • 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 7027953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:18:58+00:00 2026-05-28T00:18:58+00:00

Please consider the following URLs: http://www.mydomain.com/a/test.php https://www.mydomain.org/a/b/test.php http://www.mydomain.co.nr/a/b/c/test.php https://www.mydomain.com/a/b/c/d/test.php http://www.mydomain.co.uk/a/b/c/d/e/test.php https://www.mydomain.co.au.nm/a/b/c/d/e/f/test.php?var1=test1&var2=test2 Now I want

  • 0

Please consider the following URLs:

http://www.mydomain.com/a/test.php
https://www.mydomain.org/a/b/test.php
http://www.mydomain.co.nr/a/b/c/test.php
https://www.mydomain.com/a/b/c/d/test.php
http://www.mydomain.co.uk/a/b/c/d/e/test.php
https://www.mydomain.co.au.nm/a/b/c/d/e/f/test.php?var1=test1&var2=test2

Now I want to declare a constant called ACTUAL_URL in test.php file so that it contains the following outputs respectively (Lets assume that these URLs represent the main directory of the website, respectively in the order mentioned above):

http://www.mydomain.com/
https://www.mydomain.org/a/
http://www.mydomain.co.nr/a/b/
https://www.mydomain.com/a/b/c/
http://www.mydomain.co.uk/a/b/c/d/
https://www.mydomain.co.au.nm/a/b/c/d/e/

Now consider a file, 1.php located in the following locations:

http://www.mydomain.com/1.php
https://www.mydomain.org/a/1.php
http://www.mydomain.co.nr/a/b/1.php
https://www.mydomain.com/a/b/c/1.php
http://www.mydomain.co.uk/a/b/c/d/1.php
https://www.mydomain.co.au.nm/a/b/c/d/e/1.php

The PHP code for 1.php would be:

//1.php
//The folder name may remain as "a" or may change. 
//But for most cases, it will be "a"
require_once('a/test.php');

//Rest of the code for the page 

Few things to note:

  1. Ideally test.php will never be executed on its own. Rather, it is an include file that would be included in other files within the main directory and/or within any other files in the sub-directories.
  2. Note that the code is smart enough to detect http and https from the received url and use the same.
  3. It could be a .com domain or a .co.au.nm domain and it would still fetch it correctly.
  4. Strips out any parameters received via $_GET.

I tried the following, but failed to get a satisfactory answer:

define("ACTUAL_URL", basename($_SERVER['PHP_SELF']) );
define("ACTUAL_URL", dirname($_SERVER['PHP_SELF']) );
define("ACTUAL_URL", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] );

The first 2 dont provide the website URL, rather they seem to provide the physical path of the folder, which is not required. I need the accessible URL. The third one gives the domain name + folders (which is good) but contains the file name of the currently executing page and any $_GET params associated with it. For example, consider the following structure of my files in a website:

https://www.mydomain.co.au.nm/a/b/c/d/e/1.php?var1=test1&var2=test2
https://www.mydomain.co.au.nm/a/b/c/d/e/f/test.php

In the above case, I want the constant ACTUAL_URL to give me:

https://www.mydomain.co.au.nm/a/b/c/d/e/

Relative path to test.php will always be included in 1.php file.

So how can I achieve the above indicated functionality?

  • 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-05-28T00:19:17+00:00Added an answer on May 28, 2026 at 12:19 am

    You can use the dirname() function twice. Once to get rid of the filename (and any querystring variables) and another time to traverse up one directory.

    dirname(dirname($_SERVER["REQUEST_URI"]));
    

    Please note, this won’t work on files in the root directory of the website. All your paths will need to be at least 1 folder deep.

    Really though, this whole approach is questionable from a security standpoint. Allowing the client to pass in paths which ultimately get chopped up and passed into require_once opens your application up to path traversal attacks. If you are going to do this, make sure to validate/sanitize the input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following structure of my website: www.mydomain.com/ www.mydomain.com/index.php www.mydomain.com/404.php www.mydomain.com/css www.mydomain.com/blog/ How
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
I have one folder named test located at www.mydomain.com/abc/files/test. I need to change the
Please consider the following scenario, There are two web applications App1 & App2. A
Please consider the following code: <?php class MyException extends Exception {} function global_exception_handler($exception) {
Please consider the following code. struct foo { }; template<typename T> class test {
Please consider the following test program (using scala 2.9.0.1) object test { def main(args:Array[String])
Please consider the following: http://jsfiddle.net/CASM6/9/ I have two rows of tabs. there are two
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Please consider the following: <td style=width: 500px;> <div style=width: 400px;>SomeContent</div> </td> For some reason,

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.