Jump to content

Recommended Posts

Hello,

 

I am pretty much new to php and I am trying to figure out the following:

 

I have a URL string like:

domain.com/index.jsp;jsessionid=824E485D748EB687F88497BA39685C81

 

I am looking for someone who could help me out with a function that would be like

 

function cleanurl($word,$toclean) {  

}

 

The idea would be that I can use it like:

 

$cleanurl = cleanurl($dirtyurl,";jsessionid");

 

So what the function would do is: Check the url for ;jsessionid and remove everything after, but including the selected word / mini string.

 

Here is an example:

 

$dirtyurl = "domain.com/index.jsp;jsessionid=824E485D748EB687F88497BA39685C81";

$cleanurl = cleanurl($dirtyurl,";jsessionid");

 

echo $cleanurl woudl return:

domain.com/index.jsp

 

Any help would be great :)

 

It's a lot simpler than you might think with the substr_replace() function. Try this:

 

function cleanurl($word, $toclean)
{
    $pos = strpos($word, $toclean);

    if ($pos !== false)
    {
        return substr_replace($word, '', $pos);
    }

    return $word;
}

Thanks for that - it works fine so far.

 

I wonder if there is a different way as I just noticed that some urls break after removing the session ids if the urls are like

 

domain.com/index.php?PHPSESSID=12345&area=home

 

Is there maybe a way to just strip from the startpoint to either the next slash (/) or to the next "and" (&) so values after the removed session stay intact?

 

Again, thanks for your very quick post - It's nearly working as expected :)

You can do this easily with PEAR:

 

<?php

require_once 'Net/URL.php';

$dirtyurl = 'domain.com/index.php?PHPSESSID=12345&area=home';

$url = new Net_URL($dirtyurl);
unset($url->querystring['PHPSESSID']);

$cleanurl = $url->getURL();

?>

 

Although you may want to add "http://" to the start of your URLs to stop PEAR automatically adding the current domain you're on...

Or using regular expressions:

 

<?php
function cleanurl($url, $query_name) {
$pattern = '~(\?|&)' . preg_quote($query_name, '~') . '=[^&]*&?~';
$cleanurl = preg_replace($pattern, '$1', $url);
if (in_array(substr($cleanurl, -1), array('?', '&'))) {
	$cleanurl = substr($cleanurl, 0, -1);
}
return $cleanurl;
}
$url = 'http://domain.com/index.php?PHPSESSID=12345&area=home';
echo cleanurl($url, 'PHPSESSID');
//http://domain.com/index.php?area=home
?>

It should return a perfectly valid URL, removing ?s and &s when necessary. If your URLs use a semi-colon instead of a question mark to denote the start of the query string, you can swap the first and last question mark within the function with semi-colons.

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.