Can anyone figure-out why the ELSE gets triggered after you click the “submit” button when the script is set to trigger the THEN once you click the button ?
<?php
$conn = mysqli_connect("localhost", "root", "", "id");
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
//WHY IS NOT THE FOLLOWING BEING ECHOED ?
echo $url_to_proxify;
?>
<?php
//WHY IS NOT THE FOLLOWING CODE GETTING TRIGGERED AFTER SUBMIT BUTTON HAS BEEN CLICKED ?
$page = file($url_to_proxify);
foreach($page as $phrase)
{
//eg: $pattern = array("localhost", "./", "https://", "http://");
$phrase = preg_replace('/src="/', 'src="'.$url_to_proxify, $phrase);
$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify=' .$url_to_proxify, $phrase);
echo $phrase;
}
}
else
{
echo 'The "else" got triggered in the "if" condition!';
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Url: <input type = "text" name = "url_to_proxify" />
<input type = "submit" />
</form>
</body>
</html>
Thanks man! In 5 secs, you solved my issue that I was overlooking!
But new issue …
Can anyone figure-out why I get these errors ?
Notice: Undefined index: url_to_proxify in C:\xampp\htdocs\id\proxy.php on line 7
Warning: file(): Filename cannot be empty in C:\xampp\htdocs\id\proxy.php on line 14
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\id\proxy.php on line 15
I already included a file name and so I should not get this error.
Using the script, type “http://google.com” in the text box and then click the “submit” button. You will be redirected to google.com.
Then, search google and you will see these errors mentioned above.
<?php
$conn = mysqli_connect("localhost", "root", "", "id");
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
$url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
//WHY IS NOT THE FOLLOWING BEING ECHOED ?
echo $url_to_proxify;
?>
<?php
//WHY IS NOT THE FOLLOWING CODE GETTING TRIGGERED AFTER SUBMIT BUTTON HAS BEEN CLICKED ?
$page = file($url_to_proxify);
foreach($page as $phrase)
{
//eg: $pattern = array("localhost", "./", "https://", "http://");
$phrase = preg_replace('/src="/', 'src="'.$url_to_proxify, $phrase);
$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify=' .$url_to_proxify, $phrase);
echo $phrase;
}
}
else
{
echo 'The "else" got triggered in the "if" condition!';
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Url: <input type = "text" name = "url_to_proxify" />
<input type = "submit" />
</form>
</body>
</html>
I suspect the issue is this …
When I click the Google Search box, I am taken to a SERP that has no google domain. Now, I need to deal with this by finding a proper regex that will grab the domain name from any url. I am googling for it but too many links show different ways of doing it and so if you guys know a regex then you are welcome to mention it.
You’re missing the point. The get check will ALWAYS return true, so it always goes into it. So you either need to also check for a value in url_to_proxify or replace the two instances of GET to POST. Replacing the instances to POST would probably be better in the long run
You are thinking of $_REQUEST but are missing the point like Dave said. Get is the “normal” request method for any page, so it’s going to be true in most cases (where the request method is not something else).
Let me see if I understood you.
You are saying, the very moment the page loads, it is always getting the following to become $TRUE not matter what. Hence, no chance of the ELSE getting triggered ?
if ($_SERVER[‘REQUEST_METHOD’] == “GET”)
Now, how did I manage to mess this up ?
How would you code it so this mess does not occur ?
Thanks man!
Yeah, I did miss the point!
I forgot that, if my url contains the “?” and the “=” which are related to the GET, then I am already triggering the GET, without giving the chance for the script to use the IF condition in the script flow. (Eg. 3rd line of the script in our example).
So, what you are saying is, if I put a link to the following on my webpage: http://localhost/id/proxy.php?url_to_proxify=http://www.ebay.com
and if any visitor clicks it or if the visitor directly types that in their browser then the 3rd line will always prove to be true:
if ($_SERVER[‘REQUEST_METHOD’] == “GET”)
Hence, the GET will always get triggered. Therefore, the following line of code will be ignored or be rendered useless:
<?php
$conn = mysqli_connect("localhost", "root", "", "id");
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
$url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
//WHY IS NOT THE FOLLOWING BEING ECHOED ?
echo $url_to_proxify;
?>
<?php
//WHY IS NOT THE FOLLOWING CODE GETTING TRIGGERED AFTER SUBMIT BUTTON HAS BEEN CLICKED ?
$page = file($url_to_proxify);
foreach($page as $phrase)
{
//eg: $pattern = array("localhost", "./", "https://", "http://");
$phrase = preg_replace('/src="/', 'src="'.$url_to_proxify, $phrase);
$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify=' .$url_to_proxify, $phrase);
echo $phrase;
}
}
else
{
echo 'The "else" got triggered in the "if" condition!';
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Url: <input type = "text" name = "url_to_proxify" />
<input type = "submit" />
</form>
</body>
</html>
Now, about these errors:
Notice: Undefined index: url_to_proxify in C:\xampp\htdocs\e_id\proxy.php on line 7
Warning: file(): Filename cannot be empty in C:\xampp\htdocs\e_id\proxy.php on line 14
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\e_id\proxy.php on line 15
Looking at my code, I guess here it means the Variable has not been defined (error indicating to line 7).
But, I don’t understand how I have not defined it since line 7 looks like this:
No. Even visiting without the query parameters will trigger that IF statement to be TRUE. All visits to a page are GET by default.
Why are you using mysqli_real_escape_string? You shouldn’t be.
Yup, read this post again (it replaces mysql_real_escape_string) and validate $url has a value in your IF statement (hint: replace INPUT_GET, with $_GET["url_to_proxify"]).
If you don’t mind me asking: Do you mind simply adding your suggested code onto my code and then pasting it in this thread so I can get a clear picture what you are suggesting ?
Because in my code, I do not see any: $url.
I see instead:
Yeah, I read mysqli_real_escape_string is outdated.
Let’s see how you would change this line to what.
Best you update my script on your end and just paste it here so newbies all can be clear on what you are hinting. Not everyone can understand hints.
I changed it to your suggestion.
Anyway, what about my post 13 above about the Undefined Index error ?
Elsewhere, I just got suggested that, I should revert my code back to:
if(isset($_GET[“url_to_proxify”]) === TRUE)
Recently, I had changed the above line to this (check line 5 in post 13):
if ($_SERVER[‘REQUEST_METHOD’] == “GET”)
What is your input on this ? I reckon what I have been suggested is correct but let us see what you think before I roll off to sleep.
Check this before replying:
Once again thank you everyone who helped! Most appreciated!