Jump to content

Passing a url variable in the url


Jeffro

Recommended Posts

I had an iframe working for the last few months on a site at hostgator.  Yesterday, it quit working (403 permissions error).  After a long bout of trouble-shooting, I found out that it has something to do with mod_security that they have suddenly enabled (have no idea as I'm not a Linux guy). 

 

They told me they fixed the problem on my domain by whitelisting it as an exception, but strangely, even though the permissions error went away, the actual src= box of the iframe, which was the url variable I was passing in the url, no longer loads. 

 

So.. I'm trying to break this down into the simplest form to figure it out.  I just understand php basics so needing some verification that I'm doing this right/wrong. 

 

Here's my code.. 

 

page1.php

<? $testurl = "http://google.com"; ?>
<a href="http://mysite.blah/page2.php?url=<? echo $testurl; ?>">page2.php</a>

 

page2.php

if (isset($_GET['testurl'])) echo $testurl;
else echo "sorry dude"; 

 

I am only able to print "sorry dude". 

Am I doing something wrong or shouldn't this send the url? 

 

Thanks for the help!

 

 

 

Link to comment
Share on other sites

print_r($_GET); on page two and you will see the problem.

 

That was some fast help! 

 

Okay.. this definitely helps.. but not quite sure what it means.  I get this:

Array ( [url] => http://google.com )

Link to comment
Share on other sites

Then the code you provided in your OP is not your actual code. Neither of those snippets contains listing.

 

You also called it page2 but your code says test2.

 

(Sorry.. just updated.  I was testing with 2 different pages.. basically both doing the same thing)

Link to comment
Share on other sites

To get the ?url=<blah> from your code you use the superglobal $_GET and index it with url. So on the second page you just have

 

$url = $_GET['url'];
if(isset($url)){
    echo $url;
} else {
    echo "sorry dude";
}

Link to comment
Share on other sites

Didn't realize I didn't put that here.. but yes, I had it in there. 

 

Okay.. to avoid confusion, here is a copy/paste of my test1.php and test2.php (only the domain name has been changed):

 

test1.php:

<?php
$testurl = "mytest";
echo $testurl;
echo '<br>';
?>
<a href="http://domain.com/test2.php?testvar=<? echo $testurl; ?>">test2.php</a>

 

test2.php:

<?
$testurl = $_GET['testurl'];
?>
<html>
<head>
</head>
<body>

<? 

if (isset($_GET['testurl'])) {
echo $testurl;
echo '<br>';
}
else {
echo "sorry dude"; 
echo '<br>';
}

print_r($_GET);

?>

</body>
</html>

 

And here is the output of test2.php:

sorry dude
Array ( [testvar] => mytest )

Link to comment
Share on other sites

<a href="http://domain.com/test2.php?testvar=<? echo $testurl; ?>">test2.php</a>[/code]

sorry dude
Array ( [testvar] => mytest )

 

test2.php:

$testurl = $_GET['testurl'];

 

Testurl does not equal testvar

 

you are passing testvar as your $_GET but not looking for that var in your test2 page, instead you are looking for testurl which doesnt exist because your are using testvar.

 

either change testvar in your url link to testurl or vice versa and you'll work

<? 

if (isset($testurl)) {    // because you have already made $testurl you dont need the $_GET anymore
echo $testurl;
echo '<br>';
}
else {
echo "sorry dude"; 
echo '<br>';
}

print_r($_GET);

?>

</body>
</html>

Link to comment
Share on other sites

thanks, ajrocker..  I think that did it!

 

I guess that should have been obvious, but I honestly just rarely do php.. and only the minimal change here or there when I need it a few times a year.  I thought the name in the url (testvar=) could be anything and was just used as an identifier while the get had to match the actual $variable from the prior page. 

 

Thanks much for the help.. and the print_r command! (jesirose).  Handy!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.