Jump to content

Remove new line from encoded url string


Go to solution Solved by AbraCadaver,

Recommended Posts

Hello,

 

 

 

Code:

 

 

<?php
$mylink = $_GET["link"];
$final_link = $mylink;
 
if( strpos($mylink,"test")>0)
{
 $tmp = "mystring1".$mylink."mystring2";
 $final_link = urlencode($tmp);
 $loc = "Location: ". $final_link;
 echo $loc;
 //header($loc); 
 exit();
}
 
?>
 
 

 

I have written above php code, problem is, when i echo "loc" i get this

 

1st line : Location: 

2nd line : http

 

So, when i do header($loc), i adds "/" in between Location and http and page won't be forwaded.

 

Please help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/
Share on other sites

Try adding a few var_dump() calls:

<?php
$tmp = "mystring1".$mylink."mystring2";
var_dump($tmp); print '<br>';
 
$final_link = urlencode($tmp);
var_dump($final_link); print '<br>';
 
$loc = "Location: ". $final_link;
var_dump($loc); print '<br>';
?>
 
What does each call return?
Edited by cyberRobot

I did a test with this code and this is the result

$mylink = 'http://forums.phpfreaks.com/test';
$final_link = $mylink;
 
if(strpos($mylink,"test")>0)
{
 $tmp = "mystring1".$mylink."mystring2";
 $final_link = urlencode($tmp);
 $loc = "Location: ". $final_link;
 echo $loc;
 //or echo 'Location: '.$final_link;
 //header('Location: '.$final_link);
 //exit();
 //if the link contains "test", then it returns:
 //Location: mystring1http%3A%2F%2Fforums.phpfreaks.com%2Ftestmystring2
}else{
//otherwise it returns:
//Location: http://forums.phpfreaks.com/
echo 'Location: '.$final_link;
//header('Location: '.$final_link);
}

you can try the code on this site:

http://writecodeonline.com/php/

Location: mystring1http%3A%2F%2Fforums.phpfreaks.com%2Ftestmystring2

That is not a valid Header redirect.

 

Perhaps you need to tell us what "mystring1" actually is, and why you are urlencode'ing it. It is true that $mylink (probably) needs to be urlencode'd, but the url you are redirecting to needs to be clean.

 

 

$url = 'http://mydomain.com/redirect.php?url=' . urlencode($mylink);

Location: mystring1http%3A%2F%2Fforums.phpfreaks.com%2Ftestmystring2

 

 

That is not a valid Header redirect.

 

Perhaps you need to tell us what "mystring1" actually is, and why you are urlencode'ing it. It is true that $mylink (probably) needs to be urlencode'd, but the url you are redirecting to needs to be clean.

 

 

$url = 'http://mydomain.com/redirect.php?url=' . urlencode($mylink);

 

Yeah, I know, but without knowing the "mystring1" and "mystring2", I leave the code as he put it. xD

here is what it looks like.

 

$tmp = "track.in.omgpm.com/?AID=999&MID=999&PID=999&CID=999&WID=999&UID=999&redirect=".$mylink."?utm_source=OMG&utm_medium=affiliate&utm_campaign=AID&aff=K5MHQM6";

 

I need to encode this and forward to this address.

 

Please help

You were probably urlencoding too much, and you have added a newline by breaking the line inside quotes.  This might work unless you have other surprises:

$tmp = "track.in.omgpm.com/?AID=999&MID=999&PID=999&CID=999&WID=999&UID=999&redirect="
. urlencode($mylink . "?utm_source=OMG&utm_medium=affiliate&utm_campaign=AID&aff=K5MHQM6");
  • Solution

For completeness:

$final_link = "http://track.in.omgpm.com/?AID=999&MID=999&PID=999&CID=999&WID=999&UID=999&redirect="
. urlencode($mylink . "?utm_source=OMG&utm_medium=affiliate&utm_campaign=AID&aff=K5MHQM6");

header("Location: $final_link");
exit;

 

For completeness:

$final_link = "http://track.in.omgpm.com/?AID=999&MID=999&PID=999&CID=999&WID=999&UID=999&redirect="
. urlencode($mylink . "?utm_source=OMG&utm_medium=affiliate&utm_campaign=AID&aff=K5MHQM6");

header("Location: $final_link");
exit;

 

Yes.

 

This solution did work for me,

But i think we have to encode whole url.

 

 

 

 

 

DO we need to encode complete url or just the one you have shown is enough?

The impact is it will work if you don't encode it.  If you do encode it, it won't work because it won't be a valid URL.  Try this in your browser address bar:

http%3A%2F%2Ftrack.in.omgpm.com%2F%3FAID%3D999%26MID%3D999%26PID%3D999%26CID%3D9
99%26WID%3D999%26UID%3D999%26redirect%3D%3Futm_source%3DOMG%26utm_medium%3Daffil
iate%26utm_campaign%3DAID%26aff%3DK5MHQM6

 

 

The impact is it will work if you don't encode it.  If you do encode it, it won't work because it won't be a valid URL.  Try this in your browser address bar:

http%3A%2F%2Ftrack.in.omgpm.com%2F%3FAID%3D999%26MID%3D999%26PID%3D999%26CID%3D9
99%26WID%3D999%26UID%3D999%26redirect%3D%3Futm_source%3DOMG%26utm_medium%3Daffil
iate%26utm_campaign%3DAID%26aff%3DK5MHQM6

 

 

Ok.

 

Thanks for clearing this :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.