#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question "file(): Filename cannot be empty in ... " Error


    Guys,

    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 ?

    Code:
    <?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>

    Strange!
    Last edited by UniqueIdeaMan; June 29th, 2017 at 09:59 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question


    Guys,

    I sorted the above post's issue, with the help of another programmer, by changing the:

    if ($_SERVER['REQUEST_METHOD'] == "POST")

    to:

    if ($_SERVER['REQUEST_METHOD'] == "GET").


    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.

    Code:
    <?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.

    Thanks!
    Last edited by UniqueIdeaMan; June 29th, 2017 at 10:11 AM.
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,748
    Rep Power
    4536
    The first error creates the others. What does "undefined index" mean to you? Do you know what an index is and what data type uses them?
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question


    Originally Posted by Sepodati
    The first error creates the others. What does "undefined index" mean to you? Do you know what an index is and what data type uses them?
    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:

    $url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));

    Now, looking at this link:

    PHP - Notice: Undefined index

    It seems the best way to avoid this error is by using the "isset". But, I don't know how to isset the following:

    if ($_SERVER['REQUEST_METHOD'] == "GET")


    Anyone have any suggestion to how I can code this ?


    Another issue:
    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:
    301 Moved Permanently

    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:

    Code:
    if ($_SERVER['REQUEST_METHOD'] == "GET")
       {
    		$url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
    		echo $url_to_proxify;
    		?>	
    
    		<?php
    		$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!';
    	}
    Right ?

    Therefore, in order to not get the script jump the first 2 lines (get the GET involved in the url), I should link like this:

    **301 Moved Permanently

    And not go beyond the ".php" in the linking ? Meaning, not link like this:

    **301 Moved Permanently

    Or, am I missing the point again ?

    Here is the actual code again to jog your memory:

    Code:
    <?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>
    Last edited by UniqueIdeaMan; June 29th, 2017 at 04:50 PM.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,038
    Rep Power
    4101
    You should go back to using your original condition
    Code:
    if(isset($_GET["url_to_proxify"]) === TRUE)
    The first time your page loads is going to be a GET request, but it won't have the url parameter so that is why you get your undefined index error. You need to check if the parameter has been set before trying to do anything with it, which your original code does.

    Checking $_SERVER['REQUEST_METHOD'] === 'POST' is for detecting if a form has been submitted, but only if the form is configured to use the POST method, which yours is not.

    In general I find it best to just check for the inputs you require rather than the request method. Just because a request was done with POST for example doesn't mean all your expected form fields exist, you still have to check if they are set or you'd get the same undefined index errors.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Originally Posted by kicken
    You should go back to using your original condition
    Code:
    if(isset($_GET["url_to_proxify"]) === TRUE)
    The first time your page loads is going to be a GET request, but it won't have the url parameter so that is why you get your undefined index error. You need to check if the parameter has been set before trying to do anything with it, which your original code does.

    Checking $_SERVER['REQUEST_METHOD'] === 'POST' is for detecting if a form has been submitted, but only if the form is configured to use the POST method, which yours is not.

    In general I find it best to just check for the inputs you require rather than the request method. Just because a request was done with POST for example doesn't mean all your expected form fields exist, you still have to check if they are set or you'd get the same undefined index errors.

    Thanks buddy! I only resorted to using the $_SERVER['REQUEST_METHOD'] === 'POST'

    due to this:

    http://www.codingforums.com/php/240088-ie-if-isset-$_post[submit]-bug-explained.html#post1144368

    Do you still say I should revert back to my old code since the new one is not working ?

    Elsewhere I am told to replace:

    $url_to_proxify = trim(mysqli_real_escape_string($conn,$GET["urlto_proxify"]));

    with this:

    $url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);

    I do know that, this is outdated: mysqli_real_escape_string.

    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.

    Once again thank you everyone who helped! Most appreciated!

    Good Night! Happy Dreams!
    Last edited by UniqueIdeaMan; June 29th, 2017 at 07:10 PM.
  12. #7
  13. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,038
    Rep Power
    4101
    That thread doesn't really have anything do do with your situation, nor do I see them anywhere saying to use $_SERVER['REQUEST_METHOD'], in fact they are saying to do exactly what I just told you to do and what you were already doing: Check for the input value.

    What that thread is warning about is trying to test for the existence of the submit button in your form data. Older versions of IE would not submit the name/value pair of submit buttons unless the button was specifically clicked on. Submitting the form by pressing Enter in a text input would omit the button making any test for the button's existence fail.

    So instead of testing the button you test something else. Some people suggest testing the request method, but this only really works if your method is different for the form, such as using POST. Other's suggest testing for the actual form input values, which will exist (except for unchecked checkbox/radio inputs).

    Since your form uses the GET method, checking the request method is a bit pointless, it will bet GET whether your form was submitted or not. That leaves testing for the input as the only real choice.

    Elsewhere I am told to replace:
    You could, it gives you some validation that the input is actually a url.

    You shouldn't be using mysqli_real_escape_string unless you're going to be putting the value into SQL. It's not some generic thing you just do to all your inputs, it's something you should only do to input immediately prior to putting it into an SQL query.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,748
    Rep Power
    4536
    Originally Posted by UniqueIdeaMan
    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:

    $url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
    Wrong. It means, exactly as it says, that an index is undefined. What part of line 7 is an index?
    -- Cigars, whiskey and wild, wild women. --
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    First and foremost, "wrongfully" is not a word. As with PHP, the English language has a manual of sorts, know as a "dictionary", and though it does not define the grammar of the language, it's good for other things like seeing if a word exists. I consult it often.

    Originally Posted by UniqueIdeaMan
    Guys,

    I sorted the above post's issue, with the help of another programmer, by changing the:

    if ($_SERVER['REQUEST_METHOD'] == "POST")

    to:

    if ($_SERVER['REQUEST_METHOD'] == "GET").
    Again, I would avoid GET if at all possible.

    Originally Posted by UniqueIdeaMan
    But new issue ...

    Can anyone figure-out why I get these errors ?
    I shall take a stab at it... There is no telling if I can find your issue...

    Originally Posted by UniqueIdeaMan
    Notice: Undefined index: url_to_proxify in C:\xampp\htdocs\id\proxy.php on line 7
    $_GET["url_to_proxify"] is not defined. It's not in the form, so it's not even null, it's not there at all. The error is telling you that "url_to_proxify" (Proxify???) is NOT in the $_GET array. This is the root of the rest of the errors.

    Originally Posted by UniqueIdeaMan
    Warning: file(): Filename cannot be empty in C:\xampp\htdocs\id\proxy.php on line 14
    $url_to_proxify has no value in it.

    Originally Posted by UniqueIdeaMan
    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\id\proxy.php on line 15[/B]
    foreach($page as $phrase) fails because $page has no content because $url_to_proxify doesn't contain a file name.

    Originally Posted by UniqueIdeaMan
    I already included a file name and so I should not get this error.
    Clearly you didn't check your code step by step. Show me where you put a value in $url_to_proxify. I still think you are not getting the file name from $_GET["url_to_proxify"], and the error tells me that "url_to_proxify" (Proxify???) is NOT in the $_GET array. I think I already said that, so this is the issue you must track down.

    Lastly, good lord, man, spin up a Linux machine. Use CentOS v6 or v7 (the Open Sores version of Red Hat Enterprise, the Gold Standard of stable Linux). Who in their right minds runs a web server on Windows, are you insane? Don't answer that. And if you are planning on pushing all this spaghetti to a web server in the big bright beautiful Cloud, why not Linux? If you don't switch up your development platform now you'll have to change all the HIDEOUS Windows path names to the well thought out and elegant Linux path names when you come to your senses and embrace the beauty that is Linux... Richard Stallman told me so, so it must be true.
    Last edited by Arty Zifferelli; July 1st, 2017 at 03:45 PM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Originally Posted by Arty Zifferelli
    First and foremost, "wrongfully" is not a word. As with PHP, the English language has a manual of sorts, know as a "dictionary", and though it does not define the grammar of the language, it's good for other things like seeing if a word exists. I consult it often.


    Again, I would avoid GET if at all possible.



    I shall take a stab at it... There is no telling if I can find your issue...


    $_GET["url_to_proxify"] is not defined. It's not in the form, so it's not even null, it's not there at all. The error is telling you that "url_to_proxify" (Proxify???) is NOT in the $_GET array. This is the root of the rest of the errors.


    $url_to_proxify has no value in it.


    foreach($page as $phrase) fails because $page has no content because $url_to_proxify doesn't contain a file name.


    Clearly you didn't check your code step by step. Show me where you put a value in $url_to_proxify. I still think you are not getting the file name from $_GET["url_to_proxify"], and the error tells me that "url_to_proxify" (Proxify???) is NOT in the $_GET array. I think I already said that, so this is the issue you must track down.

    Lastly, good lord, man, spin up a Linux machine. Use CentOS v6 or v7 (the Open Sores version of Red Hat Enterprise, the Gold Standard of stable Linux). Who in their right minds runs a web server on Windows, are you insane? Don't answer that. And if you are planning on pushing all this spaghetti to a web server in the big bright beautiful Cloud, why not Linux? If you don't switch up your development platform now you'll have to change all the HIDEOUS Windows path names to the well thought out and elegant Linux path names when you come to your senses and embrace the beauty that is Linux... Richard Stallman told me so, so it must be true.
    Ha! Ha! Ha! I like your humour in the end. Yes, I use Win 10 and use Xampp on it when practicing php. Installed it about 2 mnths ago. Other than that, I at first used to test my php codes on my website (linux ofcourse) for a few months before installing Xampp.

IMN logo majestic logo threadwatch logo seochat tools logo