preetham Posted July 16, 2013 Share Posted July 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/ Share on other sites More sharing options...
.josh Posted July 16, 2013 Share Posted July 16, 2013 You can use str_replace (the examples even show replacing linebreaks) Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1440971 Share on other sites More sharing options...
preetham Posted July 17, 2013 Author Share Posted July 17, 2013 Thanks for repying. However, str_replace() is not working for me. I tried these, both didn't work str_replace("\r", '', $loc); str_replace("\n", '', $loc); Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441005 Share on other sites More sharing options...
cyberRobot Posted July 17, 2013 Share Posted July 17, 2013 The new value needs to be reassigned to the variable. http://php.net/manual/en/function.str-replace.php $loc = str_replace("\r", '', $loc); Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441045 Share on other sites More sharing options...
preetham Posted July 17, 2013 Author Share Posted July 17, 2013 The new value needs to be reassigned to the variable. http://php.net/manual/en/function.str-replace.php $loc = str_replace("\r", '', $loc); It doesn't work Please someone help. I'm using urlencode, i think it related to that. Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441105 Share on other sites More sharing options...
cyberRobot Posted July 17, 2013 Share Posted July 17, 2013 (edited) 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 July 17, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441125 Share on other sites More sharing options...
.josh Posted July 17, 2013 Share Posted July 17, 2013 It doesn't work Please someone help. I'm using urlencode, i think it related to that. Then do the str_replace before the urlencode... Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441130 Share on other sites More sharing options...
2260419 Posted July 17, 2013 Share Posted July 17, 2013 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/ Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441143 Share on other sites More sharing options...
DavidAM Posted July 17, 2013 Share Posted July 17, 2013 Location: mystring1http%3A%2F%2Fforums.phpfreaks.com%2Ftestmystring2That 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); Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441144 Share on other sites More sharing options...
2260419 Posted July 17, 2013 Share Posted July 17, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441151 Share on other sites More sharing options...
preetham Posted July 18, 2013 Author Share Posted July 18, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441283 Share on other sites More sharing options...
AbraCadaver Posted July 18, 2013 Share Posted July 18, 2013 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"); Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441285 Share on other sites More sharing options...
Solution AbraCadaver Posted July 18, 2013 Solution Share Posted July 18, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441287 Share on other sites More sharing options...
preetham Posted July 18, 2013 Author Share Posted July 18, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441288 Share on other sites More sharing options...
AbraCadaver Posted July 18, 2013 Share Posted July 18, 2013 (edited) It won't work if you encode more than that. Edited July 18, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441289 Share on other sites More sharing options...
preetham Posted July 18, 2013 Author Share Posted July 18, 2013 It won't work if you encode more than that. ok. But what is impact if i don't encode first part? Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441290 Share on other sites More sharing options...
AbraCadaver Posted July 18, 2013 Share Posted July 18, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441292 Share on other sites More sharing options...
preetham Posted July 18, 2013 Author Share Posted July 18, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280216-remove-new-line-from-encoded-url-string/#findComment-1441294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.