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

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question Warning: Invalid argument supplied for foreach()


    Folks,

    I keep getting this error no matter how differently I code:

    **Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\id\proxified_page_2.php on line 58**

    Here is my code:

    Code:
    <?php
    
    $conn = mysqli_connect("localhost", "root", "", "e-id");
    
    if (!$conn) {
    	// message to use in development to see errors
    	die("Database error : " . mysqli_error($conn));
    
    	// user friendly message
    	// die("Database error.");
    	exit();
    }
    
    ?>
    
    <html>
       <body>   
          <form action = "<?php $_PHP_SELF ?>" method = "GET">
             Url: <input type = "text" name = "url_to_proxify" />
                  <input type = "submit" />
          </form>      
       </body>
    </html>
    
    
    <?php
    
    $url_to_proxify = "";
    		
    if(isset($_GET["url_to_proxify"]) === TRUE)
       {
    		$url_to_proxify = trim(mysqli_real_escape_string($conn,$_GET["url_to_proxify"]));
    		//WHY IS NOT THE FOLLOWING BEING ECHOED ?
    		echo $url_to_proxify;
    
    
    		$url = $url_to_proxify;
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, "$url");
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    		curl_setopt($ch, CURLOPT_HEADER, 5);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    		$result = curl_exec($ch);
    		
    		$domain = parse_url($url, PHP_URL_HOST);
    		echo $domain;
    
    		//Below change 'localhost' to "./".
    		//eg: $pattern = array("./", "https://www.", "http://www.", "https://", "http://", "www.");
    		$pattern = array("localhost", "https://www.", "http://www.", "https://", "http://", "www.");
    		$replace   = array("proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=");
    
    		$phrase = str_replace($pattern, $replace, $result);
    
    
    		//Below code from Basic Php Proxy Video and fix from: https://stackoverflow.com/questions/22255241/preg-replace-no-ending-matching-delimiter-gt/22255455#22255455
    
    		foreach($url 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=' .$domain.$url_to_proxify, $phrase);
    			echo $phrase;
    			
    			curl_close($ch);
    		}
    	}
    
    ?>
    And no. It is not the same code you see in my other thread: else-triggered-wrongfully.
    That one and this script's aim is the same. To proxify pages without showing errors when making keyword searches on a search box.
    But that one is coded with file function while this one with cURL.

    How would you fix line 15 ?

    Code:
    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;
    		}
    Been trying to fix these 2-3 scripts this whole night. Other issues solved thanks to you guys and so just one more issue left. This issue.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Ok. So, to make foreach to work, how would you code it ?

    This was the original code that worked:

    Code:
    <?php
    
    //Below code from Basic Php Proxy Video and fix from: https://stackoverflow.com/questions/22255241/preg-replace-no-ending-matching-delimiter-gt/22255455#22255455
    
    $url = "http://www.google.com";
    $page = file($url);
    
    foreach($page as $part)
    {
    	
    $part = preg_replace('/src="/', 'src="'.$url,$part);
    $part = preg_replace('/action="/', 'action="'.$url,$part);
    
    echo $part;
    
    }
    
    ?>
    I got that code from:

    https://www.youtube.com/watch?v=P49w0E64MAA

    I then changed it to:

    Code:
    <?php
    $conn = mysqli_connect("localhost", "root", "", "id");
    if (!$conn) {
    	// message to use in development to see errors
    	die("Database error : " . mysqli_error($conn));
        // user friendly message
    // die("Database error.");
    exit();
    }
    ?>
    <html>
       <body>   
          <form action = "<?php $_PHP_SELF ?>" method = "GET">
             Url: <input type = "text" name = "url_to_proxify" />
                  <input type = "submit" />
          </form>      
       </body>
    </html>
    <?php
    $url_to_proxify = "";
    if(isset($_GET["url_to_proxify"]) === TRUE)
       {
    		$url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
    		//WHY IS NOT THE FOLLOWING BEING ECHOED ?
    		echo $url_to_proxify;
        	$url = $url_to_proxify;
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, "$url");
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    	curl_setopt($ch, CURLOPT_HEADER, 5);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    	$result = curl_exec($ch);
    	
    	$domain = parse_url($url, PHP_URL_HOST);
    	echo $domain;
    
    	//Below change 'localhost' to "./".
    	//eg: $pattern = array("./", "https://www.", "http://www.", "https://", "http://", "www.");
    	$pattern = array("localhost", "https://www.", "http://www.", "https://", "http://", "www.");
    	$replace   = array("proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=", "proxified_page_2.php?url_to_proxify=");
    
    	$phrase = str_replace($pattern, $replace, $result);
    
    
    	//Below code from Basic Php Proxy Video and fix from: https://stackoverflow.com/questions/22255241/preg-replace-no-ending-matching-delimiter-gt/22255455#22255455
    
    	foreach($url 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=' .$domain.$url_to_proxify, $phrase);
    		echo $phrase;
    		
    		curl_close($ch);
    	}
    }
    ?>
    Nice try on my part. Correct ?
    You see, if you check out that youtube tutorial, you will see the guy builds a basic proxy. Only 5 mins video. He proxified google homepage. There was a problem where the google img did not appear and when you clicked the search button then the page showed an error. Then he fixed the code. But he ends there, I, however, try adding a tracker to track all proxified pages. First I try proxifying every link found on the proxified page. His code does not show you how to do this. Anyway, once I managed to proxify every link on the proxified page, I would then try adding the tracker link on all proxified links. That is easy for me if I can sort this foreach issue. It appeared as soon as I made changes to the youtube tutorial code.
    Last edited by UniqueIdeaMan; June 29th, 2017 at 07:29 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,288
    Rep Power
    630
    Just out of curiosity, why do you continue to resist using the proper tags for your PHP code? Do you like making it harder for those trying (with infinite patience) to help you, read your code? If you use the proper tags it might even help you see your own silly syntax errors.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,741
    Rep Power
    4536
    What is the first parameter to foreach() supposed to be?

    PHP: foreach - Manual

    One of these days you're going to learn how to program instead of just smashing your **** on cntrl-c and cntrl-v.

    -John
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo