douruhu4282 2013-05-06 17:55
浏览 70
已采纳

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

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

  • 写回答

4条回答 默认 最新

  • dongzi0602 2013-05-06 18:32
    关注
    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿