cURL Experiments

Fellow Members,

One thing been puzzling me for days and so without wasting anymore time, I’m just gonna ask.

When you type a url in the “Url” ui text box, cURL is supposed to fetch that url.
Now, when I type: http://google.com.
And click the “submit” button. Guess what happens ? The page does not get fetched or I see a complete white blank page because the browser shows me I have been redirected to:

http://localhost/e_id/proxified_page_test.php?url_to_proxify=http%3A%2F%2Fgoogle.com

Instead of to:

http://localhost/e_id/proxified_page_test.php?url_to_proxify=http://www.google.com

Now, I am guessing this:

://www.

gets encoded to this:

%3A%2F%2F

Q1.
But why is that ? And how to fix this so user is not redirected to:

http://localhost/e_id/proxified_page_test.php?url_to_proxify=http%3A%2F%2Fgoogle.com

but redirected to what the user input:

http://localhost/e_id/proxified_page_test.php?url_to_proxify=http://www.google.com

I hope I don’t have to use the str_replace here too.


<?php

//STEP 1: ERROR HANDLING

declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');


//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

E_DEPRECATED;

error_reporting(E_ALL);

/* STEP 2:
The IF gets triggered as soon as the "submit" button is clicked in the ui text box labeled: Url
Following IF code deals with GET method.
*/

if(isset($_GET["url_to_proxify"]) === TRUE)
   {
		
		echo "IF got triggered!";
		$url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "$url_to_proxify");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$curl_result = curl_exec($ch);
		
		
		$domain = parse_url($url_to_proxify, PHP_URL_HOST);
		
        //Proxify Links, Deal with Image Files (Eg. Google Img File), Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)		
		$pattern = array("https://", "http://", "localhost", "/", 'src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='", 'action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='", "<a href=\"n", "<a href='n");
		$replace = array("proxified_page_test.php/?url_to_proxify=https://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", 'src="proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src = "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src= "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src ="proxified_page_test.php/?url_to_proxify=\"$domain\""', "src='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src = 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src= 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src ='proxified_page_test.php/?url_to_proxify=\"$domain\"'", 'action="proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action = "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action= "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action ="proxified_page_test.php/?url_to_proxify=\"$domain\""', "action='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action = 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action= 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action ='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "<a href=\'proxified_page_test.php/?url_to_proxify=http://\"$domain\"/n'", "<a href=\'proxified_page_test.php/?url_to_proxify=http://\"$domain\"/n'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		print_r($curl_result);
		curl_close($ch);			
	}
else
    {
		echo "ELSE got triggered!";
		//Html Form
		?>
		<html>
			<body>   
				<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "GET">
				Url: <input type = "text" name = "url_to_proxify" />
				<input type = "submit" />
      </form>      
   </body>
</html>

<?php
	}

?>

Q2. Is my curl_close($ch); in the right place ? Or, should it come immediately after the $curl_result = curl_exec($ch);

Q3. I am also trying to echo the $domain.
But guess what ? If I place the echo variable immediately after defining that variable then the echo fails.

$domain = parse_url($url_to_proxify, PHP_URL_HOST);
echo $domain;

Can you figure-out why it fails by checking the full script above ? It should not fail to echo!

Also, notice that, if I place it immediately after closing curl then it manages to echo the variable. Why is that ? Strange is not it ?

print_r($curl_result);
curl_close($ch);
echo $domain;

Anyway, by looking at my full code above, can it be improved in the line arrangements ? I mean, is there anything I should rearrange because my arrangement is flawed in terms of bug free arrangement or security or stability ?
How would you line-up all that ? What would be your rearrangements of my current code ?

The source of the web page I created is available if no URL is passed and has the following debug function:

  1. It is easier to type than print_r and var_dump
  2. Adding <pre> inserts line feeds for easier reading
  3. It is easy to replace print_r with var_dump
function fred( $val, $title=NULL)
{
  if($title)
  {
    $title = $title .' ==> <br>';
  }

  echo '<pre class="w88 mg1 bga bd1 p42">';
    echo $title;
    print_r($val);
  echo '</pre>';  
}///



2 Likes

The basic idea of the site is to display the hidden html control characters used to make a site look pretty. Please read about the php htmlentities function. The best place is the php online manual:

http://php.net/manual/en/function.htmlentities.php

There are four options and I suggest creating a simple ‘Hello world’ web page and pasting the URL into the input text box.

Next is to view and compare the curl, proxify and htmlentities results.

With a bit of luck you will be able to spot why the proxify routine is producing a blank page.

Try to learn the basics of a simple web page and it may help providing a solution to the blank pages.

I know working at one problem at a time is enough, so please forgive me.

Congratulations on using var_dump and gaining some insight of data types. You are on your way to learning how to think in “programming”.

However, you seem to still not be understanding some of what I’d consider to be “basic”. In particular, the error_reporting block of code. As analogy: The code is like

I want apple pie
No, I want lemon pie
No, I want cherry pie
pumpkin pie
No, I want blueberry pie

The extraneous “pumpkin pie” will be ignored.
You will get a blueberry pie

If after giving it some thought you don’t get that all that is “repeated passing of parameters to this function will override earlier lines” and “putting a CONSTANT alone on a line is useless” please speak up and I’ll try to think of a different way to say it.

4 Likes

The Online PHP Manual is quite clear:

  1. The error_reporting() function sets the error_reporting directive at runtime.

  2. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.

  3. If the optional level is not set, error_reporting() will just return the current error reporting level.

  4. Returns the old error_reporting level or the current level if no level parameter is given.)

Notice item 4, function error_level() returns an integer which can be displayed using echo error_reporting(...);

As mentioned repeatedly calling the function will just override the previous setting.

Reminds me of the following:

… doing the same thing over and over again and expecting different results.
Albert Einstein

As numerous others have said - simplify your script by not doing too many operations or statements on a single line and use var_dump($val); die; If it is not the desired result then correct the errors before moving on.

As previously mentioned try creating and echoing a function getCurlResult( $domain ) {…}

Why use echo when it has been previously stated that using var_dump($val); will return the value which may hav been 0 or and empty string which will not show $val?

Take al look at the online proxify script:


<?php
  // PHP7 SPECIFIC - REMove if not PHP7
     declare(strict_types=1);

  // ENABLE IF NOT ALREADY SET IN php.ini
     // error_reporting(-1); 
     // ini_set('display_errors', '1'); 

  $title = 'UniqueIdeaMan Proxify Routine';
  $forum = 'https://www.sitepoint.com/community/t/curl-experiments/264321';

  // ALL DEFAULT TO NULL
    $zprox          = isset($_GET['zprox']) 
                    ? 'checked' 
                    : NULL;
    $zspec          = isset($_GET['zspec']) 
                    ? 'checked' 
                    : NULL;
    $url_to_proxify = isset($_GET["url_to_proxify"]) 
                    ? $_GET["url_to_proxify"] 
                    : NULL;

  # values returned                    
    $result = NULL; 
    $msg    = NULL;
  if( $url_to_proxify )
  {
  // SET DEFAULTS 
     $result = 'NO getCurlResults( ' .$url_to_proxify .')';
     $msg    = 'NO Curl Results:';

  // VALIDATE URL
     $url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
     if($url_to_proxify){
       $result = getCurlResults( $url_to_proxify );
       $msg    = 'Curl Results:';
     }//endif;  

   // GET OUTPUT  
     if($result)
     {
       if($zprox)
       {  
         $result = get_parse_results($url_to_proxify, $result);
         $msg    = 'Proxified';
       }// enidf $check

       if($zspec)
       {
         $result = htmlentities($result, ENT_SUBSTITUTE);
         $msg    = 'HtmlEntities using <b>ENT_SUBSTITUTE</b>';
       }
     }// endif $curl_result   

  }else{ // NO ( $url_to_proxify )
    $result = highlight_file( __FILE__, true);
    $msg    = 'PHP Source Code for this web-page:';
  }//endif $url_to_proxify

  // CONVERT $results to string
     $result = print_r($result, true);


  // RENDER RESULTS
     echo getHeaderAndForm($title, $forum);
      echo showBody($url_to_proxify, $result, $zprox, $zspec, $msg);
     echo showFooter();




// ==================== ONLY FUNCTIONS BELOW ====================
// function getCurlResults( $url_to_proxify='url_to_proxify' ) {...}
// function getHeaderAndForm($title, $forum) {...}
// function showBody($url_to_proxify=NULL, $result=NULL, $zprox, $zspec, $msg='Big Problem :(') {...}
// function showFooter() {...}
// function get_parse_results($url_to_proxify, $curl_result=NULL)  {...}
// function fred($val, $title=NULL) {...}

If you stopped guessing and did a little serious thinking, you might start to make progress.

Firstly, there are only three encoded characters in your example, so they can’t be replacing seven unencoded characters.

If “www” were being encoded, would you not expect there to be three identical encoded characters, rather than two of one type and one of another?

Why would you think “www” is part of that string, when it is not in your original URL? http://google.com

Surely you can compare http://google.com to http%3A%2F%2Fgoogle.com and see which characters have been encoded?

If you’re still lost, then look them up. https://en.wikipedia.org/wiki/Percent-encoding

4 Likes

I did ask in post #226 that, I can create 2 versions of the error reporting:

1 Version


//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

2nd Version


error_reporting(E_ALL);

But I got no answer.
I thought, if I use version 2 and then delete the codes of version 1 from all my files on notepad++ and then see you guys prefer version 1 then I’d lose all those codes of version 1 and would have to google all over again (I had forgotten I can get v1 from this thread).
Therefore, on my file, I placed both versions.
And then, when I asked you guys questions regarding my script on post #227, I forgot to use either version and just copied and pasted my code from notepad++ which contain both versions and forgot to delete one version that I favour less. Hence, you see me using both versions. It was not meant to be. It was a slip of the “tongue” so to speak. Wasn’t ignoring any suggestions.
So, if you don’t mind, I’ll ask again, which version do you recommend ? Your answer would be a reply to my post #227. I’m guessing you’ll recommend v2. Right ?

And yes, I did understand you when you said you don’t want the E_DEPRECATED in between all those lines of error reporting code. Correct me if that was not the case.
So, you suggest I put it at the final line of the error reporting codes ?
http://php.net/manual/en/errorfunc.constants.php

EDIT: Looking at post #206 again, I see again and jog my memory that, you recommended v2 or the shortest version during using the code on your site. And, if I’m not mistaken, you’d use v1 if you were testing your code offline in xamp etc.

Ah! Care to show an optional level and a mandatory level error reporting codes that you yourself use ?
This is good experience gaining from someone who has very good work experience. :slight_smile:

Sorry John,

I did not understand every line of your code on your website. Seems like Intermediate stuff.
Unless ofcourse, if the lines had comments. Then, it would be a different story. I don’t think a beginner would struggle understanding it. :wink:
Let us see if your comments teach me something that I consider to have learnt 2 nay 3 nay 4 new things from your site. :slight_smile:

You seem to have missed @John_Betong’s earlier post.

You are spot on!
I guess, I got careless.
Yes, my inputted url was: http://google.com.
And yes, there were no “www” involved. So, the basic logic is, whatever %3A%2F%2F represent then it’s defintiely not “www.”
I guess, I got careless a little and assumed too much.
And yes, you are right, since there are 3 identical chars then it’s obvious the %3A%2F%2F do not represent the 3 identical chars since the 3 pairs are not identical. Especially the first pair.
And so, yes, if I had the sense to notice it then I could have used my nose as a detective or a police dog to"figure-out" what those encodings mean.
Anyway, let’s try. I can see a mental pic of Inspector Gadget getting his magnifying glass out and …he sees this:

://

Now, you may congratulate Gadget and he’ll thank you for bringing him to his sense to use his magnifying glass when he turns blind when he’s not supposed to. :wink:

However, saying all this, I don’t think his magnifying glass can see why the the UI text box encodes the :// when there was no code written to do so. Unless some function is doing it. In that case, Gadget would be might grateful if the great Inspector Sherlock Holmes can share some light.
Note that, as soon as I type in the UI text box:

and click the “submit” button. cURL loads:

http%3A%2F%2Fgoogle.com

and I see a blank page.
Now, when I replace the encodings with “://” directly in the url field on my browser and go to:

then I see a proxified page of google homepage. Spotted this about half a wk or a wk ago.
I believe I have answered John_Betongs’ answer.
If you’re reading this John, then it’s the answer to your question on #post 229.
But, I am still puzzled why the variable is encoding parts of the url. I need an explanation on that. Atleast.
I’ll see if the htmlentities have anything to do with it. Most likely not. Most likely, I’ll have to read upon the url encodings. Right ?

Reply to your question:

As numerous others have said - simplify your script by not doing too many operations or statements on a single line and use var_dump($val); die; If it is not the desired result then correct the errors before moving on.

I actually created 3 versions of my script and have been experimenting for weeks. One way or the other, I come across a variety of stumbling blocks. At the end, I created a 4th version.
The first 3, had code like this to which you are suggesting it seems:

$pattern = array("https://", "http://", "localhost");
		$replace = array("proxified_page.php/?url_to_proxify=https://", "proxified_page.php/?url_to_proxify=http://", "proxified_page.php/?url_to_proxify=");
		$phrase = str_replace($pattern, $replace, $curl_result);

		print_r($phrase);
		$pattern = array("https://", "http://", "localhost");
		$replace = array("proxified_page_2.php/?url_to_proxify=https://\"$domain\"", "proxified_page_2.php/?url_to_proxify=http://\"$domain\"", "proxified_page_2.php/?url_to_proxify=http://\"$domain\"");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with Image Files (Eg. Google Img File)		
		$pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
		$replace = array('src="proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src = "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src= "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src ="proxified_page_2.php/?url_to_proxify=\"$domain\""', "src='proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src = 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src= 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src ='proxified_page_2.php/?url_to_proxify=\"$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
		$pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
		$replace = array('action="proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action = "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action= "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action ="proxified_page_2.php/?url_to_proxify=\"$domain\""', "action='proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action = 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action= 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action ='proxified_page_2.php/?url_to_proxify=\"$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);

		print_r($curl_result);
		$pattern = array("https://", "http://", "localhost");
		$replace = array("proxified_page_3.php/?url_to_proxify=https://", "proxified_page_3.php/?url_to_proxify=http://", "proxified_page_3.php/?url_to_proxify=http://");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with Image Files (Eg. Google Img File)		
		$pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
		$replace = array('src="proxified_page_3.php/?url_to_proxify="', 'src = "proxified_page_3.php?/url_to_proxify="', 'src= "proxified_page_3.php/?url_to_proxify="', 'src ="proxified_page_3.php/?url_to_proxify="', "src='proxified_page_3.php?url_to_proxify='", "src = 'proxified_page_3.php/?url_to_proxify='", "src= 'proxified_page_3.php/?url_to_proxify='", "src ='proxified_page_3.php/?url_to_proxify='");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
		$pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
		$replace = array('action="proxified_page_3.php/?url_to_proxify="', 'action = "proxified_page_3.php/?url_to_proxify="', 'action= "proxified_page_3.php/?url_to_proxify="', 'action ="proxified_page_3.php/?url_to_proxify="', "action='proxified_page_3.php/?url_to_proxify='", "action = 'proxified_page_3.php/?url_to_proxify='", "action= 'proxified_page_3.php/?url_to_proxify='", "action ='proxified_page_3.php/?url_to_proxify='");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		print_r($curl_result);	

But then I thought, hell, why have 3 different sections and just combine them into 1:

        //Proxify Links, Deal with Image Files (Eg. Google Img File), Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)		
		$pattern = array("https://", "http://", "localhost", "/", 'src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='", 'action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='", "<a href=\"n", "<a href='n");
		$replace = array("proxified_page_test.php/?url_to_proxify=https://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", "proxified_page_test.php/?url_to_proxify=http://\"$domain\"", 'src="proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src = "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src= "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'src ="proxified_page_test.php/?url_to_proxify=\"$domain\""', "src='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src = 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src= 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "src ='proxified_page_test.php/?url_to_proxify=\"$domain\"'", 'action="proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action = "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action= "proxified_page_test.php/?url_to_proxify=\"$domain\""', 'action ="proxified_page_test.php/?url_to_proxify=\"$domain\""', "action='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action = 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action= 'proxified_page_test.php/?url_to_proxify=\"$domain\"'", "action ='proxified_page_test.php/?url_to_proxify=\"$domain\"'", "<a href=\'proxified_page_test.php/?url_to_proxify=http://\"$domain\"/n'", "<a href=\'proxified_page_test.php/?url_to_proxify=http://\"$domain\"/n'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		print_r($curl_result);

Replying to your question:

As previously mentioned try creating and echoing a function getCurlResult( $domain ) {…}

I actually did as you suggested. But I get a complete blank page and following the method I mentioned to TechnoBear above that rids the white blank page is not working on this particular version of the script, even if the method is working on other versions of the script. Check above to see what I just replied to TechnoBear.
Here is the code that shows a complete white blank page and nothing seems to rid this problem. Note, I don’t use you guy’s recent suggestions such as use var_dump etc. as this is a version I stopped working on about a wk ago. But, I’m just mentioning it here to show you that I did use your suggested:

getCurlResult( $domain ) {…}

But I did ask you later-on on another post of mine if I placed that line in the right line or not. Because, I suspect I did not. Looking at the code below, do you mind telling me this ? That way, I might solve the issue of the blank white page.

This was version 2 of the script.


<?php

//STEP 1: ERROR HANDLING


ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

/* STEP 2:
The IF gets triggered as soon as the "submit" button is clicked in the text box labeled: Url
Following IF code deals with GET method.
*/

if(isset($_GET["url_to_proxify"]) === TRUE)
   {
		function get_parse_results($url_to_proxify, $curl_result=NULL) {
		echo "IF got triggered!";
		$url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "$url_to_proxify");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$curl_result = curl_exec($ch);
		curl_close($ch);
		
		$domain = parse_url($url_to_proxify, PHP_URL_HOST);
				
		$pattern = array("https://", "http://", "localhost");
		$replace = array("proxified_page_2.php/?url_to_proxify=https://\"$domain\"", "proxified_page_2.php/?url_to_proxify=http://\"$domain\"", "proxified_page_2.php/?url_to_proxify=http://\"$domain\"");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with Image Files (Eg. Google Img File)		
		$pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
		$replace = array('src="proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src = "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src= "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'src ="proxified_page_2.php/?url_to_proxify=\"$domain\""', "src='proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src = 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src= 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "src ='proxified_page_2.php/?url_to_proxify=\"$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		
		
		//Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
		$pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
		$replace = array('action="proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action = "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action= "proxified_page_2.php/?url_to_proxify=\"$domain\""', 'action ="proxified_page_2.php/?url_to_proxify=\"$domain\""', "action='proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action = 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action= 'proxified_page_2.php/?url_to_proxify=\"$domain\"'", "action ='proxified_page_2.php/?url_to_proxify=\"$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);

		print_r($curl_result);
		
		
       
		}
		
	}
else
    {
		echo "ELSE got triggered!";
		//Html Form
		?>
		<html>
			<body>   
				<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "GET">
				Url: <input type = "text" name = "url_to_proxify" />
				<input type = "submit" />
      </form>      
   </body>
</html>
<?php
	}

?>

Replying to your question:

Why use echo when it has been previously stated that using var_dump($val); will return the value which may hav been 0 or and empty string which will not show $val?

Oops! I forgot to make use of the var_dump on version 4 of the script to which you are referring to. I had actually added that echo when you suggested it a few days earlier before SamA74 or somebody suggested the var_dump.
Check your post #183. I’m afraid my memory was stuck at your post #183 or my code was.

Nah! I forgot about that post.
I see now, him and Mittineague prefer this:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

error_reporting(E_ALL);

But, I’ll ask you, where should I put the E_DEPRECATED ?
I asked that to someone just an hr ago.
Might aswell leave it at the end. Since I am new and not fully aware of what has been deprecated and since I pinch codes from youtube videos that have outdated codings from time to time then I guess that line would become handy for me. What do you think ?
I don’t think it would be overidden if I place it at the end. But, it won;t override the others, right ? I mean, it looks like a different set from any of the 2:

//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);
error_reporting(E_ALL);

This time, I concatenated the $domain like so:

//Add proxy link on all links present on proxified page
$pattern = array("http://", "https://", "http://www.", "https://www.", "localhost");
$replace = array("proxified_page_test.php?url_to_proxify=http://\".$domain\"", "proxified_page_test.php?url_to_proxify=https://\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=https://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"");
$string_replaced_data = str_replace($pattern, $replace, $curl_result);
echo var_dump($string_replaced_data);

		
//Add proxy link on all Image Links (Eg. Google Img File)		
$pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
$replace = array('src="proxified_page_test.php?url_to_proxify=\".$domain\""', 'src = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src ="proxified_page_test.php?url_to_proxify=\".$domain\""', "src='proxified_page_test.php?url_to_proxify=\".$domain\"'", "src = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
$string_replaced_data = str_replace($pattern, $replace, $curl_result);
echo var_dump($string_replaced_data);

		
//Add proxy link on all links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
$pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
$replace = array('action="proxified_page_test.php?url_to_proxify=\".$domain\""', 'action = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action ="proxified_page_test.php?url_to_proxify=\".$domain\""', "action='proxified_page_test.php?url_to_proxify=\".$domain\"'", "action = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
$string_replaced_data = str_replace($pattern, $replace, $curl_result);
echo var_dump($string_replaced_data);

Note that “.” (dot) before the “$domain”.

But still, when I do a google search, I am taken to this invalid url:

http://localhost/search?ie=ISO-8859-1&hl=en&source=hp&biw=&bih=&q=gg&btnG=Google+Search&gbv=1

It should have been:
http://localhost/**e_id/proxified_page_test.php?url_to_proxify=http://google.com/**search?ie=ISO-8859-1&hl=en&source=hp&biw=&bih=&q=gg&btnG=Google+Search&gbv=1

Why is not the highlighted parts (check the opening and closing asterisks in the url above) getting preceded to all the links found in the proxified page ?
Look at the str_replace. According to that, the preceding should have occured.
Can you spot any errors in my str_replace function usage ? If not, then the str_replace is not doing it’s job.

Can you spot any errors in my $domain concatenation code ?
If not, then neither the $domain is getting concatenated into the urls. Why the “dot” (.) failing to concatenate ?

The echo var_dump($domain) is managing to print the domain. That means, the $domain variable is not blank. I can see it holding a value but that value is not taking part in the replacements. And so, why is the $domain failing to get added to the urls of the links presented on the proxified pages ? Weird, ha ?

Here’s the complete code:

<?php

//STEP 1: ERROR HANDLING

declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');


//For All Error, Warning and Notice
error_reporting(E_ALL);
E_DEPRECATED;

/* STEP 2:
The IF gets triggered as soon as the "submit" button is clicked in the ui text box labeled: Url
Following IF code deals with GET method.
*/

if(isset($_GET["url_to_proxify"]) === TRUE)
   {
		
		echo "IF got triggered!";
		$url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "$url_to_proxify");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$curl_result = curl_exec($ch);		
		
		$domain = parse_url($url_to_proxify, PHP_URL_HOST);
		echo var_dump($domain);
				
		//Add proxy link on all links present on proxified page
		$pattern = array("http://", "https://", "http://www.", "https://www.", "localhost");
		$replace = array("proxified_page_test.php?url_to_proxify=http://\".$domain\"", "proxified_page_test.php?url_to_proxify=https://\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=https://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		echo var_dump($string_replaced_data);
		
		//Add proxy link on all Image Links (Eg. Google Img File)		
		$pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
		$replace = array('src="proxified_page_test.php?url_to_proxify=\".$domain\""', 'src = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src ="proxified_page_test.php?url_to_proxify=\".$domain\""', "src='proxified_page_test.php?url_to_proxify=\".$domain\"'", "src = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		echo var_dump($string_replaced_data);
		
		//Add proxy link on all links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
		$pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
		$replace = array('action="proxified_page_test.php?url_to_proxify=\".$domain\""', 'action = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action ="proxified_page_test.php?url_to_proxify=\".$domain\""', "action='proxified_page_test.php?url_to_proxify=\".$domain\"'", "action = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
		$string_replaced_data = str_replace($pattern, $replace, $curl_result);
		echo var_dump($string_replaced_data);

		print_r($curl_result);
		curl_close($ch);
		
        		
	}
else
    {
		echo "ELSE got triggered!";
		//Html Form
		?>
		<html>
			<body>   
				<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "GET">
				Url: <input type = "text" name = "url_to_proxify" />
				<input type = "submit" />
      </form>      
   </body>
</html>

<?php
	}

?>

And, I still have the question:
Is my curl_close($ch); in the right place ? If not then where should I place it ?

Or, should I do the replacements like this:


$pattern = array('action="', 
$replace = array('action=".proxified_page_test.php?url_to_proxify=\".$domain\""', 

Note the concatenation dot before the “proxified_page_test.php?”.

Please be more specific by giving details of exactly what you did not understand.

1 Like

Look at the example script I supplied and notice how I pass a parameter to getCurlResults() to find the returned result, which is used later.

I prefer this method because it isolates script blocks and is easier to debug.

Once the function is “bulletproof” it can be added to a library.

The error_reporting CONSTANTs can be used in a lot of different ways depending on what you want. But at this stage I recommend you use only E_ALL - as the name implies, “all” includes all of the errors, warnings, notices, etc. There is no need to use any of the others unless you want to fine-tune what gets reported.

As for the URL encoding, after years of working with decimal (used for HTML entities), hex, and octal, I can recognize them and even remember some of the frequently used, but I still need to refer to a chart to find out what character most of them are.

2 Likes

It won’t override anything if you put it at the end, nor will it do anything, because you have failed to understand how it works. A moment’s thought might have set you on the right track, especially if you had troubled to pay attention to @Mittineague’s analogy.

But rather than waste another dozen posts getting you to try to work it out for yourself, I will spell it out for you.

E_DEPRECATED is one of the possible values of error_reporting, so simply writing it on its own will have no effect. You can set error_reporting(E_DEPRECATED), if you have a reason for doing so, but error_reporting(E_ALL) reports all error types (unsurprisingly), so as you’ve now been told numerous times, that is the only option you need to use.

2 Likes

Yes, it seems you did not understand the pie analogy.
I will try another analogy, but this time using code that can be visualised, CSS code in fact.
I know this is not a CSS project, but this is an analogy, as this works in the same way.

Likewise we could do this with PHP variables:-

$MyNumber = '1' ;
$MyNumber = '2' ;
$MyNumber = '3' ;
$MyNumber = '8' ;

echo $MyNumber ;

What is the value of $MyNumber ?

Clue: Setting error_reporting() multiple times will not give you multiple types or error reporting, it will only ever be set to one value.

3 Likes