0

我的网址来自:locahhost/index1.php?option=com_lsh&view=lsh&event_id=xxxxx&tv_id=xxx&tid=xxxx&channel=x

当用户单击此链接时,文件index1.php应处理此 URL 然后生成

这种形式的新 URL localhost/static/popups/xxxxxxxxxxx.html 其中 xxxxxxxxxxxx 是

event_id、tv_id、tid 和 chanel。

为此,我在文件中使用解析 url 函数,index1.php如下所示:

<?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");
?>

但它不起作用我认为这是由于出现问题 $url = 'http://localhost/index1.php?option=com_lsh&view=lsh&event_id=&tv_id=&tid=&channel=';

这有什么问题?我也想要它,例如当 tv_id 不存在于 url 中时,它在 newurl 中放置了 0

4

4 回答 4

1

$newUrl格式不正确。您在.]之后缺少一个右括号$data['tv_id'

$newurl = 'http://localhost.eu/static/popups/'.$data['event_id'].$data['tv_id'.$data['tid'].$data['channel'].'.html';

于 2013-05-06T18:05:02.127 回答
0

parse_url 函数是获取给定的 URL 并将其转换为它的组成部分。您正在寻找的是访问 $_GET 数组中的变量。

我假设你的事件 ID 是一个整数

$event_id=(int)$_GET['event_id'];
$new_url=''http://localhost.eu/static/'.$event_id // and so forth

如果您希望在变量之一中使用文本而不是数字,请对其进行更多清理。

于 2013-05-06T18:03:52.643 回答
0

您忘记关闭 $new_url 中的 tv_id 数组标签

$newurl = 'http://localhost.eu/static/popups /'.$data['event_id'].$data['tv_id'].$data['tid'].$data['channel'].'.html';
于 2013-05-06T18:05:45.330 回答
0
$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;

返回:

http://localhost.eu/static/popups/0000.html

更新:您不需要创建 $url 变量并将其解析为值数组。当用户单击链接时,数据会附带GET方法。如果您使用GETorPOST代替 $url,只需使用 $_REQUEST['variable'](或 $_GET[''] 或 $_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;
于 2013-05-06T18:32:39.780 回答