How To Concatenate A $domain Into a $url?

Guys,

How to concatenate a $domain into a $url ?

I am trying like this:


<?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 = 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);
		}
	}

?>

Note the following in line 64:


$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify=' ".$domain"".$url_to_proxify", $phrase);

Not working. Get this error:

Parse error: syntax error, unexpected ‘"’, expecting ‘,’ or ‘)’ in C:\xampp\htdocs\id\proxified_page_2.php on line 64

Tried even like this but no luck:


$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify=' .$domain.$url_to_proxify, $phrase);

The above is correct ? Right ?

I have read on concatenation in the php tutorials.
Just incase you’re wondering: The script is on a file called: proxified_page_2.php.

Because you wrote invalid PHP.
You really should take a course on an introduction to PHP, maybe through Code Academy (its free)

$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify='.$domain.$url_to_proxify.'"', $phrase);
1 Like

Thanks. But I thought my final sample was correct ? How incorrect was I ? Let’s see a sample to jog my memory.

I saved your link. Gonna check it out tomorrow. Way past fed time now! :wink:

Thanks!

Sample? Look at your line and compare it to mine, what differences do you see?

1 Like

Mine:

' ".$domain"".$url_to_proxify", $phrase);

Your’s:

'.$domain.$url_to_proxify.'"', $phrase);

I can see today (while wide awake) the differences between your code and mine.
Lastnight, I was half asleep and when you mentioned your following code in your reply (post #2), I didn’t realize your were giving me the answer. I thought you were just mentioning my code that you deem wrong:

$phrase = preg_replace('/action="/', 'action="proxy.php?url_to_proxify='.$domain.$url_to_proxify.'"', $phrase);

I can clearly see now, I missed-out on a dot after the “$domain.$url_to_proxify” and I missed a double quote too after the single quote and another single quote after all this.

No need to reply.

1 Like

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