Solved

Parse URL Variable

Posted on 2007-11-27
10
1,883 Views
Last Modified: 2013-12-12
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
Comment
Question by:Maricopa-IT
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
 
LVL 17

Expert Comment

by:nplib
Comment Utility
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
 
LVL 15

Expert Comment

by:babuno5
Comment Utility
check this function out it should solve your problem
http://in.php.net/parse_url
0
 

Author Comment

by:Maricopa-IT
Comment Utility
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
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Maricopa-IT
Comment Utility
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
 
LVL 17

Expert Comment

by:nplib
Comment Utility
how about you post your code so we can show you.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 225 total points
Comment Utility
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
 

Author Closing Comment

by:Maricopa-IT
Comment Utility
Thanks for all your help! Working using JavaScript as you suggested.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 34
PHP Curl Setup Option 4 28
Problem to go to Web page 2 46
uploading multiple image with php 14 14
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now