Nov 22, 2009

Can A PHP Page Knows Who's Linking To Itself?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Can A PHP Page Knows Who's Linking To Itself?

whistle
Some webpage with frames that each can be assigned to link to an url path, i.e. http://a.domain/test.php. When webpage A is requested by a browser, another webpage B containied in the frame of webpage A will be queried too.

As we know, a phpinfo() can tell us the information about the url of the webpage we get or post. When a webpage "B" is requested by the get or the post from a frame, can webpage B know the url where the webpage A locates?

I hope to know that because I wich webpage B can only be used in the frame of webpage A. If anyone request it directly, the webpage B return a something else.
For example, if someone save webpage A in his harddisk, the url of the saved page will not be the original one where the webpage A is. Thus webpage B won't be shown correctly in the frame of the saved webpage.

Can anyone tell me whether webpage B can know the url of webpage A? And how?
Or is there any other way to reach the same objective?

Comment/Reply (w/o sign-up)

calixt
QUOTE (whistle @ Aug 10 2005, 08:40 AM)
...Can anyone tell me whether webpage B can know the url of webpage A? And how?
Or is there any other way to reach the same objective?
*

whistle,

if I understood you right, you can use javascript to reach your objective.
Example:
Page A (iframe_a.html)
CODE
<html><head><title>Mainpage</title></head>
<body><h2>Mainpage</h2>
<iframe src="iframe_b.html" style="width:80%; height:50%"></iframe>
</body></html>

Page B (iframe_b.html)
CODE
<html>
<head><title>iFrame</title>
</head>
<script language="javascript">
[tab][/tab]document.writeln("Calling page (link): "+parent.location.href+"<br />");
[tab][/tab]document.writeln("Calling page (host): "+parent.location.hostname+"<br />");
[tab][/tab]document.writeln("Calling page (path): "+parent.location.pathname);
</script>
<body>...</body></html>


Just compare your.domain to parent.location.hostname to decide what to do with the page request
If parent.location.pathname has "iframe_b.html" then the iframe has been called directly.

Hope this is what you meant
calixt

 

 

 


Comment/Reply (w/o sign-up)

Hercco
A PHP page can know from which page th euser came into it, ie. in which page the link was located when the user clicked it.

Browsers send the pages address in the HTTP request, it is labeled HTTP referer and you can read it from the HTTP_REFERER global variable. ($_SERVER['HTTP_REFERER']wink.gif

There is a slight problem however... As the information is dependent on the browser, it is not always reliable, someone can easily send fake HTTP requests with bogus referers and some browsers and firewalls block the referer sending.

So be careful when using HTTP_REFERER.

Comment/Reply (w/o sign-up)

whistle
Hi, calixt:
I do test your example as following:

QUOTE (calixt @ Aug 10 2005, 05:09 PM)
Example:
Page A (iframe_a.html)
CODE
<html><head><title>Mainpage</title></head>
<body><h2>Mainpage</h2>
<iframe src="iframe_b.html" style="width:80%; height:50%"></iframe>
</body></html>

Page B (iframe_b.html)
CODE
<html>
<head><title>iFrame</title>
</head>
<script language="javascript">
[tab][/tab]document.writeln("Calling page (link): "+parent.location.href+"<br />");
[tab][/tab]document.writeln("Calling page (host): "+parent.location.hostname+"<br />");
[tab][/tab]document.writeln("Calling page (path): "+parent.location.pathname);
</script>
<body>...</body></html>


Just compare your.domain to parent.location.hostname to decide what to do with the page request
If parent.location.pathname  has "iframe_b.html" then the iframe has been called directly.
*


Finally, I get see a blank iframe and there is no desirable output. My example is http://reurl.net/see/test.htm. I just know javascript a little. May you give me more detail or tell me what's wrong with it? Than you.

Comment/Reply (w/o sign-up)

whistle
QUOTE (Hercco @ Aug 10 2005, 10:26 PM)
A PHP page can know from which page th euser came into it, ie. in which page the link was located when the user clicked it.

Browsers send the pages address in the HTTP request, it is labeled HTTP referer and you can read it from the HTTP_REFERER global variable. ($_SERVER['HTTP_REFERER']wink.gif

There is a slight problem however... As the information is dependent on the browser, it is not always reliable, someone can easily send fake HTTP requests with bogus referers and some browsers and firewalls block the referer sending.

So be careful when using HTTP_REFERER.
*


Thank you. I do test the HTTP_REFERER, it does show the right url of the webpage link to it. I have tried the $_server variable, there is no string containing "HTTP_REFERER". You give me the exact way to do the right thing. Thank you very much.

Comment/Reply (w/o sign-up)

whistle
QUOTE (whistle @ Aug 12 2005, 02:10 AM)
Hi, calixt:
  I do test your example as following:
Finally, I get see a blank iframe and there is no desirable output. My example is http://reurl.net/see/test.htm. I just know javascript a little. May you give me more detail or tell me what's wrong with it? Than you.
*


After I save the webpage as a file in my harddisk, and open the saved file. It shows as following

Calling page (link): file:///C:/Documents%20and%20Settings/1/My%20Documents/test.htm
Calling page (host):
Calling page (path): /C:\Documents%20and%20Settings\1\My%20Documents\test.htm ...

I have to mention one think, I use the os version is tranditional Chinese. So some chinese words are cover to be the strings with "%".

It is strange, the same code in the web server shows nothing. However, the right contents can be shown in a local saved file. Can anyone tell me why? I do care about "why", not only the solusion. Thanks.

Comment/Reply (w/o sign-up)

calixt
QUOTE (whistle @ Aug 11 2005, 08:10 PM)
Hi, calixt:
  I do test your example as following:
Finally, I get see a blank iframe and there is no desirable output. My example is http://reurl.net/see/test.htm. I just know javascript a little. May you give me more detail or tell me what's wrong with it? Than you.
*

Hi whistle,
when the source of the iframe does not belong to the same host as the main page, it will not have access to the "parent" object. This policy shall prevent cross site scripting.
Anyway, for your purposes it does not make any difference: If parent.location.href is not "http://reurl.net/see/test.htm" then the iframe src has not been called by the proper page.

Of course you can try to use $_SERVER["HTTP_REFERER"] but this can be faked as Hercco already mentioned. But if $_SERVER["HTTP_REFERER"] is not set then the iframe source surely has NOT been called by your Page A, and that is exactly the information you need, if I understood you right.
Greetings from Nuremberg (Germany),
C.

Comment/Reply (w/o sign-up)

whistle
QUOTE (calixt @ Aug 12 2005, 03:25 AM)
Hi whistle,
when the source of the iframe does not belong to the same host as the main page, it will not have access to the "parent" object. This policy shall prevent cross site scripting.
Anyway, for your purposes it does not make any difference: If parent.location.href is not "http://reurl.net/see/test.htm" then the iframe src has not been called by the proper page.
*


Thank you. You have clearly answer me what I hope to know. Now I see and learn a lot. You do really understand what I say although I use a poor English. Thanks again.

Comment/Reply (w/o sign-up)

JUDGE_RELIC
I was going to my nickles worth but it's just restating what has already been posted... good job guys!

Comment/Reply (w/o sign-up)

aria
If you anytime want to know any data in any array(eg. $_SERVER)you should use:
<pre>
<?php echo var_dump($_SERVER); ?>
</pre>

or maybe my own little code bit:

<?php
foreach($_SERVER $a AS => $b){
echo htmlentities($a)." <b>:</b> ".htmlentities($b)."<hr>";
}
?>

Note: I have'nt test the script!

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Php Who39s Linking


    Looking for php, page, whos, linking

See Also,

*SIMILAR VIDEOS*
Searching Video's for php, page, whos, linking
advertisement



Can A PHP Page Knows Who's Linking To Itself?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com