Solved

Parse URL Variable

Posted on 2007-11-27
10
1,919 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20362353
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
ID: 20362497
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
ID: 20362740
check this function out it should solve your problem
http://in.php.net/parse_url
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Maricopa-IT
ID: 20363681
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
ID: 20363730
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
 

Author Comment

by:Maricopa-IT
ID: 20363803
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
ID: 20366938
how about you post your code so we can show you.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 225 total points
ID: 20367121
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
ID: 31411323
Thanks for all your help! Working using JavaScript as you suggested.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20377371
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change Wording in Wordpress Plugin 4 39
Do not understand error message 3 46
Windows 10, 64 bit WAMP - won't start 9 28
Position image fpdf 4 18
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

734 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