I have URL in this from: locahhost/index1.php?option=com_lsh&view=lsh&event_id=xxxxx&tv_id=xxx&tid=xxxx&channel=x
when the user click this link, the file index1.php
should process this the URL then produce
new URL in this form localhost/static/popups/xxxxxxxxxxx.html wher xxxxxxxxxxxxx is the
event_id, tv_id, tid and chanel.
to do this I am using parse url function in the file index1.php
as following :
<?php
$url = 'http://localhost/index1.php?option=com_lsh&view=lsh&event_id=&tv_id=&tid=&channel=';
$parsed = parse_url( $url );
parse_str( $parsed['query'], $data );
$newurl = 'http://localhost.eu/static/popups/'.$data['event_id'].$data['tv_id'].$data['tid'].$data['channel'].'.html';
header("Location: $newurl");
?>
but its not working i think this is due to something wrong in $url = 'http://localhost/index1.php?option=com_lsh&view=lsh&event_id=&tv_id=&tid=&channel=';
what is wrong with this? also i want it when for example tv_id not present in the url it put instead 0 in the newurl
$url = 'http://localhost/index1.php?option=com_lsh&view=lsh&event_id=&tv_id=&tid=&channel=';
$parsed = parse_url( $url );
parse_str( $parsed['query'], $data );
$keys = array('event_id', 'tv_id', 'tid', 'channel'); // order does matter
$newurl = 'http://localhost.eu/static/popups/';
foreach ($keys as $key)
$newurl.= empty($data[$key])?0:$data[$key];
$newurl.='.html';
echo $newurl;
returns:
http://localhost.eu/static/popups/0000.html
UPDATE: You do not need to make an $url variable and parse it into an array of values. When user clicks a link data comes with GET
method. If you use GET
or POST
instead of $url, just use $_REQUEST['variable'] (or $_GET[''] or $_POST[''])
$keys = array('event_id', 'tv_id', 'tid', 'channel'); // order does matter
$newurl = 'http://localhost.eu/static/popups/';
foreach ($keys as $key)
$newurl.= empty($_REQUEST[$key])?0:$_REQUEST[$key];
$newurl.='.html';
echo $newurl;
The parse_url function is to take a given URL and turn it into its constituent parts. What you're looking for is to access variables from the $_GET array.
I'll assume your event ID is an integer
$event_id=(int)$_GET['event_id'];
$new_url=''http://localhost.eu/static/'.$event_id // and so forth
If you expect text instead of numbers in one of your variables, do some more sanitzation on it.
$newUrl
is malformed. you're missing a close-bracket ]
after $data['tv_id'
.
$newurl = 'http://localhost.eu/static/popups/'.$data['event_id'].$data['tv_id'.$data['tid'].$data['channel'].'.html';
you forgot to close the tv_id array tag in $new_url
$newurl = 'http://localhost.eu/static/popups /'.$data['event_id'].$data['tv_id'].$data['tid'].$data['channel'].'.html';