Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I encrypt my url string? 2

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
0
0
US
Hello!

I am working on a project where I was asked to take a "very long" url string and shorten it. Some times the url string can reach several hundred characters.

So, instead of having a url that looks like
Code:
[URL unfurl="true"]http://www.mysite.com/page.php?a=foo&b=foo&c=foo&d=foo&e=foo&f=foo[/URL] .....

looking for something like
Code:
[URL unfurl="true"]http://www.mysite.com/page.php?uxd823sll[/URL] or
[URL unfurl="true"]http://www.mysite.com/page.php?key=uxd823sll[/URL]

it does not have to be that short but ... you get the idea!

Thank you all for your assistance!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
the answer really is that you cannot easily shorten any text string if you need the values to be maintained. i.e. if you need it to remain meaningful. but you can

1. prettify the url as Keith suggests. so it would look like var/val/var2/val2 etc and use php or htrewrites to parse it internally for you.

2. use post instead of get (and this would be my preference)

3. use a server based bitly-esque implementation. So for every 'long' link that you are about to serve to a page you call a function that looks a bit like this

Code:
function getShortUrl($page, $queryParams = array()){
 if(!is_array($queryParams)):   
   parse_str($queryParams, $_queryParams);
   $queryParams = $_queryParams;
 endif;
 if(session_id() == '') session_start();
 $array = range('a','z'); 
 do{   
    $id = '';
    for($i = 0; $i<6; $i++) $id .= array_rand($array);
 }while(isset($_SESSION['shortUrls'][$id]));
 $_SESSION['shortURLs'][$id] = $queryParams;
 return '/'.$page.'/shortcode/'.$id;
}
/* 
* example
* echo getShortUrl('mypage.php','a=foo&b=foo&c=foo&d=foo&e=foo&f=foo');
*/

then you would have an http rewrite rule (in your .htaccess file) that looked like this (NB not tested)

Code:
RewriteEngine on
RewriteRule ^(.*?)/shortcode/(.*)$ $1?sc=$2 [NC,L]

and then have a decoder.php file that you include in every script/page

Code:
<?php
if(session_id() == '') session_start();
if(isset($_GET['sc']):
  if(isset($_SESSION['shortURLs'][$_GET['sc'])):
    $queryParams = $_SESSION['shortUrls'][$_GET['sc'];
    $_GET = array_merge($_GET['sc'], $_SESSION['shortUrls'][$_GET['sc']]);
  endif;
endif;
?>
(you can safely run this for all pages because it will only touch the $_GET superglobal when there is the sc variable set.)

you could improve this by adding some timeouts to shortURLs but it may be that the SESSION limitation is innately adequate. The code assures uniqueness of the shortcode (at least to roughtly 167 million possibilities) and if that is not enough you could change the 6 to an 8 or whatever in the for loop.
 
Very nice!

Halfway into the project specs changed so I ended needing to be able to send a "link" via email to a user and the ability to copy/paste the URL so the use of "session" would not work since the person receiving the email will start a brand new session thus not having preset values available.

That being said, I came up with:
1. get a random ID using rand()
2. md5() the random ID
3. created a file with the resulting md5 above
4. dumped in the file the URL info with other associated string values
5. sent the link out as [the md5 from above]
6. php will check for matching file and content based on $_REQUEST['key']

That is pretty much the way I ended up working it out!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

SouthBeach said:
1. get a random ID using rand()
Random may be enough when you need a very limited amount of values. If you have a few thousand targets to send e-mails to, then the chance of picking the same random number twice is quite big. See at least [tt]uniqid()[/tt] too.


Feherke.
feherke.ga
 
The use of sessions just means that the uniqueness is more likely with shorter strings.

There is no need for the session aspect really. So you could use the above code with minimal changes.

uniqid would probably be better than the array_rand mechanism.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top