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:


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


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. :wink:

Thanks

I would bet that $result is not an array, and is a string, thus the result in $phrase is a string, therefore, foreach will not work and is unnecessary.

1 Like

Ok. So, to make foreach to work, how would you code it ?

This was the original code that worked:


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

I then changed it to:


<?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 = 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.

From the PHP Manual, file return an array, each line in the file is an item in the array.

Your curl implementation is returning the whole file as a giant string. Thus no need for the foreach. Just remove the foreach and have the preg_replace lines immediately following the str_replace lines.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.