• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1934
  • Last Modified:

Parse URL Variable

Hello Experts!

I have the following URL example:

www.mysite.com/download/pdf.php?file=12345.pdf

I am pulling and can see the address bar info in an iFrame/page using:

<?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . "<br />";
//shows me the entire address: www.mysite.com/download/pdf.php?file=12345.pdf
?>

I have also trimmed it down to this:

<?php
echo "Referer: " . basename($_SERVER['HTTP_REFERER']) . "<br />";
//shows me only pdf.php?file=12345.pdf
?>

NOW MY QUESTION:
I want to return ONLY the '12345.pdf' variable information.

Normally I would simply use <?php echo $_GET['file']; ?> and be done with it. However, in an iFrame/page I cannot call it.

Thanks!
0
Maricopa-IT
Asked:
Maricopa-IT
  • 4
  • 3
  • 2
  • +1
1 Solution
 
b0lsc0ttIT ManagerCommented:
Maricopa-IT,

If you are getting this from an iframe then it needs to be clientside script.  PHP is on the server and won't work.

Javascript is the best bet.  If I assume the URL only has the one "variable" then use ...

var strFile = self.frameName.location.search.split("=")[1];

That will put the 12345.pdf value in a Javascript variable called strFile.  An example of using it when the page loads is ...

<script type="text/javascript">document.write strFile</script>

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
nplibCommented:
to do it in the iframe do this.

url is www.mysite.com/download/pdf.php?file=12345.pdf

<?php
$filename = $_GET['file'];
?>
<html>
...bunch of code..

<iframe src="somepage.php?file=<?=$filename?>"></iframe>

<./html>

then in somepage.php

<?php
echo $_GET['file'];
?>
0
 
babuno5Commented:
check this function out it should solve your problem
http://in.php.net/parse_url
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Maricopa-ITAuthor Commented:
b0lsc0tt,

Forgive my ignorance or newbieness...

I understand and would like to use JavaScript. However, can you please provide an example on how my page would like rather than just the snippet? Simply, here's what I have so far:

<title>Test</title>
</head>

<body>

<script type="text/javascript">
var strFile = self.frameName.location.search.split("=")[1];
document.write strFile

</script>

</body>
</html>

Please help. I have increased points.
0
 
b0lsc0ttIT ManagerCommented:
Where is the frame in your sample above?  Maybe I misunderstood.  The example assumed the script was in the main page and looked at the URL for a frame on that page.

If there is no frame, like in the example above use ...

<html>
<title>Test</title>
</head>

<body>

<script type="text/javascript">
var strFile = window.location.search.split("=")[1];
document.write strFile

</script>

</body>
</html>

If the URL for the page above has a query string (e.g. ?something=1234) then the script will print 1234.

I hope this clears it up.  Let me know if you have a question.

bol
0
 
Maricopa-ITAuthor Commented:
b0lsc0tt,

Allow me to explain it another way...

The main target page (or page I want users to see when they click on a PDF doc), appears on the browser address bar is:

www.mysite.com/download/pdf.html?file=12345.pdf

The page, pdf.html, simply has an IFRAME which calls a different page on another server (www.otherserver.com/pdfs/redirect.php) which is hosting my PDF library.

It's with this 'redirect.php' page that is in the IFRAME that  I wish to parse the value on the URL (in this case 12345.pdf) and have it redirect it to the actual document which will open.

The whole purpose here is to open PDFs from a different server which needs to be transparent to users.

Hopes this helps.

I'm glad I'm not the only one up so late!

Thanks!







0
 
nplibCommented:
how about you post your code so we can show you.
0
 
b0lsc0ttIT ManagerCommented:
Thanks for explaining the setup of the page and frame.  Since the script will be in the main page and the query string is part of that page's URL then ...

window.location.search.split("=")[1];

... should be exactly what you need.  However if you want to open it in the iframe then don't use document.write.  The best method would probably be to use the body's onload event for this main page.

How does redirect.php on the other domain/site expect to get the pdf to open.  I assume the URL for the iframe would be something like ...

http://www.otherserver.com/pdfs/redirect.php?thePDF=12345.pdf

Let us know exactly what you need.  Then the Javascript would be used to redirect the frame to that URL.  If you need specific help with getting the script/code for this then you have to answer the question above and let us see the iframe tag.  A sample would be ...

<body onload="self.framename.location.href = 'http://www.otherserver.com/pdfs/redirect.php?thePDF=' + window.location.search.split('=')[1];">

Let me know if you have a question or how this helps.

bol
0
 
Maricopa-ITAuthor Commented:
Thanks for all your help! Working using JavaScript as you suggested.
0
 
b0lsc0ttIT ManagerCommented:
Great.  I'm curious about the grade then.  It seems like my comments were complete and thorough but the grade was a B.  Did you still have a question or need help with this?  If so let me know and I will be happy to provide more help and info if I can.  It is nice to have a chance to earn the A or at least know why it wasn't.  In this case it seems like it would've been with the complete answer and explanation I provided.

I did see this is your first question and you are new here.  If you are curious about a "guide" for grading then take a look at a section in Help on it (http://www.experts-exchange.com/help.jsp#hi73).  Let me know if you have a question or need more info.  I am mainly following up on the grade to make sure you don't still need help but it is also good to know that hard work is rewarded. :)

Thanks for the fun question.  If you didn't mean to close this or there was a mistake in the grade then let me know.

bol
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now