php reload page with updated query string

2019-09-08 18:05发布

let say I have an url like this

http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj

Now i want to update p2=100 and reload the page using php

here parameters can be unlimited (p1,p2,...pn), and we can update any param and reload the page.

6条回答
Emotional °昔
2楼-- · 2019-09-08 18:42

Try below codes:

$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = preg_replace('/p2=([0-9]*)&/', 'p2=100&', $varURL);
header('location:'.$varNEwURL);

OR

$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = $varURL.'&p2=100';
header('location:'.$varNEwURL);
查看更多
贼婆χ
3楼-- · 2019-09-08 18:49

Fairly simply, you can do this

$_GET['p2'] = 100;
header("Location: http://www.domain.com" . $_SERVER['REDIRECT_URI'] . '?' . http_build_query($_GET));
查看更多
ら.Afraid
4楼-- · 2019-09-08 18:55

Here is what I use when I want to change 1 $var value and then redirect.

function getUrlWithout($getNames){
  $url = $_SERVER['REQUEST_URI'];
  $questionMarkExp = explode("?", $url);
  $urlArray = explode("&", $questionMarkExp[1]);
  $retUrl=$questionMarkExp[0];
  $retGet="";
  $found=array();
  foreach($getNames as $id => $name){
        foreach ($urlArray as $key=>$value){
            if(isset($_GET[$name]) && $value==$name."=".$_GET[$name])
                unset($urlArray[$key]);
      }
  }
  $urlArray = array_values($urlArray);
  foreach ($urlArray as $key => $value){
      if($key<sizeof($urlArray) && $retGet!=="")
          $retGet.="&";
      $retGet.=$value;
  }
  return $retUrl."?".$retGet;
} 

This takes the url ($_SERVER['REQUEST_URI']), removes the desired values ($getNames) [which can be one or more values], and rebuilds the url. It can be used like-

$newurl = getUrlWithout(array("p2"));
header( 'Location: http://www.domain.com/'.$newurl.'&p2=100' );
查看更多
霸刀☆藐视天下
5楼-- · 2019-09-08 18:57

Reload your page you just have to setup your variables the way you want it in the URL field

查看更多
太酷不给撩
6楼-- · 2019-09-08 18:58

If you want to reload page with desired parameters use JS

Following script might help you

window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
window.location = "http://www.domain.com/myscript.php?p2=200&p1=dfgb&p3=asdhahskh&etc=alotofparameters"

Now if you want to reload the page after a specific amount of time interval then you can use the following meta tag

<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">

Njoy Coding. :)

查看更多
We Are One
7楼-- · 2019-09-08 19:04

The question is kind of vague, but assuming you want to reload from the client side using javascript:

window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
查看更多
登录 后发表回答