php reload page with updated query string - php

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.

Fairly simply, you can do this
$_GET['p2'] = 100;
header("Location: http://www.domain.com" . $_SERVER['REDIRECT_URI'] . '?' . http_build_query($_GET));

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"

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

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.
:)

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' );

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

Related

Post url in form with get method

I have a problem today!
I am trying to post a URL in form via GET method
When I post URL it automatically converts to http://example.com/?url=http%3A%2F%2Fanonylinq.com%2F%3Fi%3Dphpphp from http://anonylinq.com/?i=phpphpIs there any way to solve this problem? I am doing this via PHP.
because I want to echo "i" as - <?php echo $i; ?> Everything else is done but I am stuck at this point.
Already done this too -
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
if you want to go this road:
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
you should use
$urlSplitted = explode('%3Fi%3D', $_GET['url']); $i = $urlSplitted[1];
Take a look at http://php.net/manual/en/function.urldecode.php
That should do the trick for it.
e.g.
$string = $_GET['url'];
$decoded = urldecode($string);
$urlSplitted = explode('?i=', $decoded );
$i = $urlSplitted[1];
I have done this:
Just made form with post method to other file having meta refresh and echoed url in meta refresh value! Thats it! Meta refresh will not encode your url.

Simple PHP url Help needed

I am new to php, I need small help
I m creating a page in php named index.php
i want when someone view that page automatically a number or anything would be added to the url end
Like www.abc.com/index.php ---> www.abc.com/index.php?abc or ?132
when ever that index page is refreshed it should get a number or any variable in the end
Try this:
<?php
if (!isset($_GET["123"])) {
header("Location: " . $_SERVER["PHP_SELF"] ."?123");
}
$QS = $_SERVER["QUERY_STRING"];
$URL = "http://www.example.com/index.php";
// Check if Anything already assigned
if( empty($QS) ) {
// Generate Any RANDOM Number Here
$NUM = mt_rand(999, 9999);
// Reload Page and assign number
header("Location: {$URL}?{$NUM}");
}
Place this code in the very top of your page
Give this a go
<?php
$num = '123';
if(!isset($_GET[$num])){
header("Location: /path/to/page?$num");
}

php header instant redirect if specified campaign urls are visited

I might worrying way too much here, but here it goes...
I have set up google analytic campaign tracking URLs using the google campaign link builder.
Which is great, but I have like 20 different tracking urls, and more to come.
These URLs are really ugly and I'm not a fan of the visitor seeing this long tracking url on there first visit to my site.
So this is my idea/theory that I want to put on my wordpress site. If anyone would be kind enough to help with the php writing part of it, I would be very grateful. Or any advice if the idea is a bad one.
For example, these are just some of my URL's
http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=Promo
http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=Promo
http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=Promo
I would like to instantly redirect all of the above urls too...
http://example.com/
Using php in my header.php or functions.php
Is there some how this can be written so I can simply add new tracking urls in an array/case perhaps.
Any suggestions would be great thanks!
Please don't laugh as this - but at a guess this is what I'm trying to do...
$landing = $_SERVER['REQUEST_URI'];
$campaigns = array(
"http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=Promo"
);
if ( $campaigns == $landing ) {
header( 'Location: http://example.com/' ) ;
}
Try this,
this is pure javascript to clear utm_params from url after load the page.
function clear_utm_from_url(){
var currentLocation = window.location.search.replace(/\?/g, '').split('&');
var new_url = new Array();
for( i = 0; i < currentLocation.length; i++){
var q = currentLocation[i].split('=');
if( q[0].search("utm_") ){
new_url.push(currentLocation[i]);
}
}
if( new_url.length > 0 ){
new_url = "?"+new_url.join("&");
}else{
new_url = "";
}
history.pushState({}, "", window.location.pathname+new_url);
}
And add something like this, or others to run function
<body onload="clear_utm_from_url();">
You should not do that.
Google analytics tracking code is client side. It is a piece of js code which executes once the page is loaded. If it does not find utm_* query parameters, nothing is going to be sent to ga, so the stats you are going to see there won't be readable. Most probably you will just see the grand total in the ga ui.
If you want to do it anyway...
$trackingParams = array('utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term');
$queryParams = array();
parse_str($_SERVER['QUERY_STRING'], $queryParams);
if (count(array_intersect($queryParams, $trackingParams)) > 1) {
// This url has tracking params
foreach ($trackingParams as $paramName) {
unset($queryParams[$paramName]);
}
// You can use a regex, but i wouldn't recommend it
$newUrl = str_replace(
$_SERVER['QUERY_STRING'],
http_build_query($queryParams),
$_SERVER['REQUEST_URI']
);
header('Location: ' . $newUrl, true, 301);
exit;
}
This is code is going to search for utm_* params in current url and redirect to an url without them, leaving other query params intact.
A simple method for removing the query string:
if ($urlIsTracker) {
list($url) = explode('?', $_SERVER['REQUEST_URI']);
header("Location: $url", true, 301);
exit;
}

PHP parameter without value using for navigation

I am trying to set up a navigation system that uses GET parameters with no value, here's an example:
http://foo.bar/?mainPage
takes the visitor to the main page.
So I am using if statements and I am wondering if there is a way I can do this without a bunch of if statements? I don't think switch statements would work for this. This is currently what I have:
$mainPage = $_GET['mainPage'];
$contact = $_GET['contact'];
if(isset($mainPage)) {
// go to main page
}
if(isset($contact)) {
// go to contact page
}
I don't want to have to end up writing a ton of if statements, though. Any ideas?
Thanks.
You could use $_SERVER['QUERY_STRING'], which gives everything in the URL after the question mark, then just use a switch statement to choose which page.
I'd suggest using an associative array:
$location_map = array(
'mainPage' => 'some-url-here',
'contact' => 'some-other-url-here'
);
foreach($location_map as $key => $value)
{
if(array_key_exists($key, $_GET))
{
header('Location: ' . $value);
exit();
}
}
//if we reached here, then redirect to home page
You can use a header redirect like so:
<?php
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>

Remove a value from a querystring

I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.
Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.
So, the querystring looks like this:
http://www.foo.com/script.php?bar1=green&bar2=blue
But when a link or 'remove' button is clicked by a user, bar1=green is removed, and the visitor is directed to the following URL:
http://www.foo.com/script.php?bar2=blue
I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.
Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpage by a PHP wrapper using an iframe.
Any suggestions greatly appreciated.
Many thanks,
Matt
You can remove the value from the query string using this code:
<?php
function parseQueryString($url,$remove) {
$infos=parse_url($url);
$str=$infos["query"];
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
if(isset($op[$remove])){
unset($op[$remove]);
}
return str_replace($str,http_build_query($op),$url);
}
echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
?>

Categories