单击链接时将URL变量传递给php

时间:2013-05-06 17:55:29

标签: php url

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

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

此表格中的新网址localhost / static / popups / xxxxxxxxxxx.html wher xxxxxxxxxxxxx是

event_id,tv_id,tid和chanel。

要执行此操作,我在文件index1.php中使用parse url函数,如下所示:

<?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 个答案:

答案 0 :(得分:1)

$newUrl格式不正确。您在]之后错过了一个近距离$data['tv_id'

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

答案 1 :(得分:0)

parse_url函数是获取给定的URL并将其转换为其组成部分。 你要找的是从$ _GET数组中访问变量。

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

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分: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方法。如果您使用GETPOST而不是$ 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;
相关问题