Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP equivalent of JS document.location

Status
Not open for further replies.

needphphelp

Programmer
Joined
Mar 2, 2004
Messages
5
Location
EE
OK, crack this. I call it 100 dollar PHP question

Can anyone translate this piece of javascript into PHP?
<script>
document.write(document.location);
</script>

It should return the current document URL, including:
query - after the question mark ? (I would use $QUERY_STRING)
fragment - after the hashmark # - this is what I actually need!

REQUEST_URI doesn 't return # or anything right to it.
Is it possible at all to do this in PHP?
 
This will return the document path/name
Code:
echo $_SERVER['PHP_SELF'];
This will show the query string
Code:
echo $_SERVER['QUERY_STRING'];

As for the '#'. Are you trying to get the HTML anchor from the URL?
 
>As for the '#'. Are you trying to get the HTML anchor from the URL?

Exactly. This is what I need. Too long to explain why, but I will be generating sections on the fly. If the visitor requests aruba.php#waterfalls

And the <a name=waterfalls> isn't there, it will be created filled with appropriate info and the page scrolled to this section.
 
Hmm.. only partly there with this one I am afraid.

The function parse_url() returns an associative array of the constituent parts of a URL including the fragment.

Code:
<?php

$url = parse_url("[URL unfurl="true"]http://www.mydomain.com/page.html#foo");[/URL]

echo $url['fragment'];

?>
Would echo the word "foo"

But..
I can't find a way to dynamically pick off the fragment from a url entered by a user.
None of the functions I know for examining a URL seem to have the fragment part.
I did read somewhere that this may or may not get passed by different browsers, and in addition the parse_url() function does not work on relative URLs.
 
I give up. I think it 's impossible to grab the fragment from url entered by user. I 'll have to use javascrpt then. Any other solution?
 
Is the fragment part of the URL handled by the browser anyway? It strikes me that it's the browser that does the work so it makes sense if the fragment isn't sent to/back from the server anyway. And if the fragment never reaches the server there is no way PHP could do anything with it.

You want the browser to jump to a section of the page determined by the URL that came as a request FROM that browser. The more I think about it, the more it seems that this would be all client side anyway.

Perhaps JavaScript would be the best way to do it.

There is something nagging at the back of my mind though.
Where do these requests originate?
If the user requests a URL then they must have either:
a) clicked a link containing the anchor
b) made up the anchor themselves

If we assume it's option a. Then that link was generated somehow, so why not store the anchor as a parameter and pass it along in the URL.
Then on the recieving page create the anchors dynamically based on the parameter.

If your sections are being generated dynamically from a database you could add an anchor at the top of each section that matches, for example, the record ID.
Then any link generated that points to that section gets the ID appended as an anchor.

Example.
We have a product, an Umbrella, in a database with an ID of '1345'.
We have a page with a link to that product, we create the link with PHP something like this,

Code:
<a href="products.php#<?=$id;?>?anchor=<?=$id;?>Umbrella</a>

Then on the page that has the Umbrella

Code:
<?
echo ("<a name=".$anchor."></a>");
echo ("<h1>Umbrella</h1>");
echo ("<p>Keeps you dry</p>");
?>


There's probably a gaping hole in that idea... I just made it up as I was typing :)
Hopefully it will kick off some lateral thinking though.
 
>Where do these requests originate?

From other websites.
I want to use this for reseller tracking. It 's meant to be a secret, but hell...
I can use the standard sell.php?reseller=john
but in this case
sell.php?reseller=john
sell.php?reseller=adam
sell.php?reseller=eve
will be considered different pages by search engines and if the pages are identical I 'll pick the ban for dupplicate content. (Maybe not), but the most important if I use
sell.php#john
sell.php#adam
sell.php#eve
All the links will be considered to point to the same page, thus I also gain some "link popularity" and better rankings.
That's extra sales in addition to what resellers generate.

Now I see why nobody has implemented this, too damn hard. However I did crack this, the solution is a combination of apache url rewrite, php, cookies and javascript.
It won 't look nice, but it'll work. Unfortunately it requires resellers to list all the URLs they will be using and visitors to enable Javascript.
 
BTW: it 's pitty, I can 't redirect with apache or PHP without loosing the ancor '#section2'.

Need to redirect
mydomain.com/oldurl/view.php?page1#section2
to
mydomain.com/newurl/view.php?page1#section2
 
That's probably because browsers don't send fragment identifiers ("#someidentifier") to web servers. The fragment identifier only makes sense within the browser's rendering of the web page.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214: Yes, I found out the hard way that browsers don't send anything after the fragment. There is no way around it.

This probably won't help, but in my case I was pulling information from a database and I needed to pass it to another page as a parameter.

I had to do this in the source page ($data[0] contains '#' in it's contents):
Code:
$newurl = ereg_replace("#","{pound}",$data[0]);

And in the page I was passing it to i had to do this:
Code:
$myvar = ereg_replace("{pound}","#", $channel);

Then I was able to pass the '#' character as part of the URL.

In this particular case it does not help... it can't redirect that type of request. However, you may be able to use the method above to pass around information between pages.

D
 
I just realized I didn't mention exactly HOW that works, sorry.... :-(

The first page generated an url like
and in the second page the $channel variable was set prior to the ereg_replace() statement by:
Code:
$channel = $_POST['value'];

After the "$myvar = ereg_replace("{pound}","#", $channel);" statement was executed, the variable #channel contained "adi#" like I had wanted it to.

Sorry, it's a REALLY crude example. I'll try to think things out more next time :-)

D
 
Argh.

The variable #myvar would contain "adi#".

My brain just shut off today for some reason.

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top