/    Sign up×
Community /Pin to ProfileBookmark

How To Echo Own Page Url On Page ?

Php Folks,

My url is this:
https://localhost/Templates/Pagination_Template.php?search=keyword&tbl=links&col=keyword&max=1&page=36

Q1.
If I want to echo on page the url in this following format then how do I do it ?
https://localhost/Templates/Pagination_Template.php?search=keyword&tbl=links&col=keyword&max=1&page=1

Q2.
If I want to echo on page the url in this following format then how do I do it ?
https://localhost/Templates/Pagination_Template.php

I ask because, when I use this:

[code]
echo $_SERVER[‘PHP_SELF’];
[/code]

I get echoed this:
**/Pagination_Template.php**

And, when I use this:

[code]
echo $_SERVER[‘PHP_SELF’];
[/code]

I get echoed this:
**Pagination_Template.php**

Q3.
This is relative url:
**Pagination_Template.php**

But what kind of url is this as it is neither rel or absolute url ?
**/Pagination_Template.php**

I need all three questions addressed.

Thanks

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@plurisistemasJun 04.2021 — I have this doubt too!

[discador automático](https://www.plurisistemas.com/discador-automatico/)
Copy linkTweet thisAlerts:
@NogDogJun 04.2021 — > @developer_web#1632534 But what kind of url is this as it is neither rel or absolute url ?

> /Pagination_Template.php


If that's what it's outputting, then it thinks Pagination_Template.php is in the root directory of the web site. (The leading "/" indicates the "document root" of the site.)
Copy linkTweet thisAlerts:
@php-bgraderJun 05.2021 — Try running a phpinfo() and look at all the variations such as

_SERVER["SERVER_NAME"] domain.com

_SERVER["SCRIPT_URL"] /phpinfo.php

_SERVER["SCRIPT_URI"] http://domain.com/phpinfo.php

_SERVER["HTTP_HOST"] domain.com

_SERVER["SCRIPT_FILENAME"] /phpinfo.php

_SERVER["QUERY_STRING"] a%5B%5D=%3Ca

_SERVER["REQUEST_URI"] /phpinfo.php?a%5B%5D=%3Ca

_SERVER["SCRIPT_NAME"] /phpinfo.php
Copy linkTweet thisAlerts:
@developer_webauthorJun 05.2021 — @NogDog#1632548

Correction:

I ask because, when I use this:
<i>
</i>echo $_SERVER['PHP_SELF'];

I get echoed this:

**/Pagination_Template.php**

And, when I use this:
<i>
</i>echo basename(__FILE__,'');

I get echoed this:

**Pagination_Template.php.**
Copy linkTweet thisAlerts:
@developer_webauthorJun 05.2021 — @php-bgrader#1632561

**Apache Environment**

SERVER_NAME localhost

SERVER_ADDR 127.0.0.1

REMOTE_ADDR 127.0.0.1

SCRIPT_FILENAME C:/xampp/htdocs/Templates/Pagination_Template.php

QUERY_STRING search=keyword&tbl=links&col=keyword&max=1&page=36

REQUEST_URI /Templates/Pagination_Template.php?search=keyword&tbl=links&col=keyword&max=1&page=36

SCRIPT_NAME /Templates/Pagination_Template.php

**PHP Variables**

$_SERVER['SCRIPT_FILENAME'] C:/xampp/htdocs/Templates/Pagination_Template.php

I do not see, even with CTRL+F:

**_SERVER["SCRIPT_URL"]

_SERVER["SCRIPT_URI"]

_SERVER["HTTP_HOST"]**
Copy linkTweet thisAlerts:
@developer_webauthorJun 05.2021 — @NogDog#1632548

And, just why does it falsely assume such ?
Copy linkTweet thisAlerts:
@NogDogJun 05.2021 — > @developer_web#1632590 And, just why does it falsely assume such ?

Well, if I had to bet on whether it's PHP or you that is making a false assumption, my money would not be on PHP. Since I'm not testing your code in your environment, I have no idea where the file is actually located, how it's being called/included by the application, etc.; so I'm not going to make any assumptions myself as to what is going on.
Copy linkTweet thisAlerts:
@developer_webauthorJun 09.2021 — @NogDog#1632596

Root directory is here:

**C:xampphtdocs**

Correct ?

And "Templates" folder is in root directory:

**C:xampphtdocsTemplates**

Correct ?

And the File is located here (**remember from my oop**):

**https://localhost/Templates/Pagination_Template.php?search=keyword&tbl=links&col=keyword&max=1&page=36**

Now, I deleted all the php code from that file just now.

And wrote only these 2 lines:
<i>
</i>&lt;?php
echo $selfpage = $_SERVER['PHP_SELF']; echo '&lt;br&gt;';
echo basename(__FILE__,'');
?&gt;


I get echoed:

**/Templates/Pagination_Template.php

Pagination_Template.php**


So yes, it is assuming the "Templates" folder is in root directory.

I think everything is fine now. It's just I forgot that when a path starts with "/" it assumes it's starting from the root directory.

Q1. Anything else you want to add here, mate ?

Or, is everything fine now ?

It's just, I thought this:
<i>
</i>&lt;?php
echo $selfpage = $_SERVER['PHP_SELF'];
?&gt;


Would echo this:

**Pagination_Template.php**

Echo echo no root directory nonsense or symbol **"/"**.

But the following code is echoing that instead. Echoing what I really want.
<i>
</i>&lt;?php
echo basename(__FILE__,'');
?&gt;

Echoing exactly what I want (just the file name):

**Pagination_Template.php**

And so, I have now decided that, if I want to echo just the file name with no path details such as root "/" then I just gonna use this code:
<i>
</i>&lt;?php
echo basename(__FILE__,'');
?&gt;

Q2. Am I gonna do the right thing here ? The code, I mean.

And, if I want to echo the root directory path, like so:

**/Templates/Pagination_Template.php**

then I will just use the code:
<i>
</i>&lt;?php
echo $_SERVER['PHP_SELF'];
?&gt;

Q3. Am I gonna do the right thing here ? The code, I mean.

But note that, I don't really like this echoed:

**/Templates/Pagination_Template.php**

Instead, I prefer to echo like following (if I gonna echo the full url with the path and not just the file name):

**https://localhost/Templates/Pagination_Template.php**

Q4. So, which code would echo like that ? NogDog. Tell me atleast that. Echo the full url, like I just shown you above. Url starting with the 'domain' if on the web. Else, starting with the "localhost".

Like so:

**https://localhost**.

Don't forget to answer all 4 questions, especially Q4. Ok ?

Thanks!
Copy linkTweet thisAlerts:
@NogDogJun 09.2021 — Untested:
[code=php]
echo "https://{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']}";
[/code]


> @developer_web#1632728 Don't forget to answer all 4 questions, especially Q4. Ok ?

Nope, you don't pay me enough.
Copy linkTweet thisAlerts:
@developer_webauthorJun 19.2021 — @NogDog#1632741

Lol! I meant, when you get the time.
Copy linkTweet thisAlerts:
@developer_webauthorJun 19.2021 — @NogDog#1632741

I am clapping my hands. You solved my problem. 😃
Copy linkTweet thisAlerts:
@iamkajalJun 25.2021 — To get the current page URL in PHP Try this code

&lt;?php <br/>
if(isset($<EM>_SERVER['HTTPS']) &amp;&amp; $_</EM>SERVER['HTTPS'] === 'on') <br/>
$url = "https://"; <br/>
else <br/>
$url = "http://"; <br/>
// Append the host(domain name, ip) to the URL. <br/>
$url.= $_SERVER['HTTP_HOST'];
<br/>
<i> </i><CODE>// Append the requested resource location to the URL
<i> </i>$url.= $_SERVER['REQUEST_URI'];
<i> </i>
<i> </i>echo $url; </CODE>
?&gt; <C>

another way:
</C>&lt;?php <br/>
$curPageName = substr($<EM>_SERVER["SCRIPT_NAME"],strrpos($_</EM>SERVER["SCRIPT_NAME"],"/")+1); <br/>
echo "The current page name is: ".$curPageName; <br/>
echo "&lt;/br&gt;"; <br/>
?&gt;


**Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.** (this is the second and last time)
Copy linkTweet thisAlerts:
@developer_webauthorJul 01.2021 — @iamkajal#1633336

Thanks Kajal!

You seem to be a worthy programmer. Lookout for my threads.; They are worthy questioning ones too!
Copy linkTweet thisAlerts:
@developer_webauthorJul 18.2021 — @NogDog#1632741

How-about I pray for you instead of paying you ?

To get saved from the Corona/Covid. NogDog.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...