Dayzza
Dayzza

Reputation: 1557

How to extract session ID from URL?

Hi is it possible to get embedded session id from url using php?

From the root url, http://www.sbstransit.com.sg/mobileiris/, the website will generate a session id which is between the url and become something like that.

i.e http://www.sbstransit.com.sg/mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx.

Isit possible to use php/any other ways to retrieve "ts2k1e55xaah50iwodsvjy35" out by querying the root url without actually physically going into the url?

Upvotes: 0

Views: 1064

Answers (2)

jrn.ak
jrn.ak

Reputation: 36619

<?php
    $url = 'http://www.sbstransit.com.sg/mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx';
    $url_arr = parse_url($url);
    print_r($url_arr);  // debug output
    $tokens = explode('/', $url_arr['path']);
    print_r($tokens);  // debug output
?>

Output:

Array
(
    [scheme] => http
    [host] => www.sbstransit.com.sg
    [path] => /mobileiris/(ts2k1e55xaah50iwodsvjy35)/index.aspx
)

Array
(
    [0] => 
    [1] => mobileiris
    [2] => (ts2k1e55xaah50iwodsvjy35)
    [3] => index.aspx
)

So you could get your session id with $tokens[2]

Upvotes: 0

tadmc
tadmc

Reputation: 3744

If you use wget to get that page, you'll see:

...
HTTP request sent, awaiting response... 302 Found
Location: http://www.sbstransit.com.sg/mobileiris/(xidluk550vzs5045l1cxkh55)/index.aspx [following]

Which indicates that it is doing a 302 redirect to the URL containing the ID.

You can write Perl (or other) code to find the redirected URL:

#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->requests_redirectable([]); # don't follow any redirects

my $response = $ua->get('http://www.sbstransit.com.sg/mobileiris/');
my $loc = $response->header('Location');

print "redirected to=$loc\n";

Upvotes: 2

Related Questions