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 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄