PHP append variable to url in address bar - php

I would like to know if its possible to append a variable to url in address bar when page loads.
demo: http://communityimpact.com/discussions/
Current page is geolocated, if category = Central Austin...would like the url to load as http://communityimpact.com/discussions/central-austin
<?php echo $my_category; ?> = Central Austin
<?php echo $my_category_slug; ?> = central-austin
Any suggestions?

Actually headers are your best bet if you need to append variables to url.
Example:
$my_category = "Central Austin";
$my_category_grub = "central-austin";
header("location: community-impact/discussions/$my_category_grub");
//or append a var like this:
header(" location: community-impact/discussions/?location=$my_category_grub");
//then you could handle the request on the new page
But both of these methods would redirect you.
If you want to use $_SESSION vars would make more sense. I don't see any possibility of appending vars to URL without redirecting. Url is the address, if you change the Url then you HAVE to change page. But session vars live until they leave your site. And can hold that info hope this helps, I'm not sure what your purpose is.
$_SESSION["category"] = "central-austin";
But you have to have
session_start();
at the beginning of the page.

Try to use a redirection with headers, like
header("Location: http://communityimpact.com/discussions/central-austin");

Your probably should use JavaScript, there are two options here:
With a redirection
// similar behavior as an HTTP redirect
window.location.replace("http://communityimpact.com/discussions/central-austin");
// similar behavior as clicking on a link
window.location.href = "http://communityimpact.com/discussions/central-austin";
Without redirection

Related

How to get URL parameter and redirect after

Please I need help with how to write php code to get a URL parameter from and redirect to another page after. My URL looks something like this http://mywebsite.com/login.php?referrer=forum
The parameter I am interested in is the referrer=forum
I need this because i am trying to integrate a forum into my website which i am almost done with except for the single sign on(SSO) feature which allows the forum to use the register and login system i have already created for my website.
Thanks.
Something like this:
<?php
// get parameter from URL params
$referrer = $_GET['referrer'];
// redirect to another URL, including the referrer above
header('Location: http://someotherwebsite.com/?referrer=' . $referrer);
?>
You can store that parameter value in variable and pass in the link or you can use Session to use that variable across the multiple page.
<?php
session_start();
$_SESSION['referrer'] = $_GET['referrer'];
?>
And then in second page just use it from the session
<?php
session_start();
echo $_SESSION['referrer']; // use it as per your requirement
?>

Hide ?ref string in URL but pass it to script

How can I hide ?ref string from users but pass it to php code so it will grab it?
For example, visitor visits this page:
http://mysite.com/?ref=ref+string
In browser URL I want to hide it, so user will see"
http://mysite.com/
But I want to grab content of ref string via this:
$ref = $_GET['ref'];
Is it possible?
No, if you want to use GET variables, they will always be in the url.
However, you can use an alternative, like $_SESSION or $_POST variables.
You could try something like this at the top of your PHP script:
session_start();
if (isset($_GET['ref'])) {
$_SESSION['ref'] = $_GET['ref'];
header('Location: /');
exit();
}
You would have to use $_SESSION['ref'] to access the value from then on, however.
This is not how the http protocol works with query strings. If you have information that needs to be in the query string that you want to hide or obfuscate from the user, I would recommend a simple encryption to change it. If you're attempting to get rid of this information for aesthetic reasons, you will need to pursue a different method of generating the header or storing the information (session/cookies/etc).

Detect redirected visitor

Here is the scenario:
Visitor of Page1.php is being redirected with JavaScript to Page2.php
Is there a way to know that visitor which lands on Page2.php is a redirected visitor by monitoring Page2.php if I don't use any sessions and variables at all in any language?
Without Doing/Using:
URL Manipulation
Cookie
Session
Any kind of Variables
Absolutely no changes to Page1.php
I'm asking this because I don't want other sites to detect that I have redirected users to their website.
I just want to know the possibility.
Just set a flag in the query string when you redirect (append the query string to your redirect location):
Page2.php?redirect=1
Or if you need the referring page:
Page2.php?referer=Page1.php
Then check with $_GET['referer']
You might be able to read the $_SERVER['HTTP_REFERER'], but I personally tend to avoid it because it doesn't always contain what you think it should.
If you don't want to use server-side languages, your only alternative is JavaScript. You could redirect to Page2.php?redirected=true and use the following code to GET the redirected variable on Page2.php.
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
if($_GET['redirected']){
// Redirected from Page1.php!
}
Source: how to get GET and POST variables with JQuery?
Set a javascript cookie on the initial page when you do the redirect.
On the new page, check to see if the cookie is set, then delete it.

Change URL using PHP

e.g. i have page with url http://mysite.com?page=3&var=10 also there is form on page.
When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was submitted somehow is there way compatible with all browsers trough PHP without mod_rewrite?
This is an old topic, but just in case anyone else is searching for this in the future, you can use the javascript replaceState to change the history and browser bar label. A simple php function to do this:
function set_url( $url )
{
echo("<script>history.replaceState({},'','$url');</script>");
}
Then would simply call this function with the desired url (presumably dropping the post variables):
set_url("http://example.com");
A page reload or a back after calling another page will now have the new url location in the history.
I think that using POST may be a more elegant solution, but if you must use GET this is a work around.
If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10 will be automatically removed.
If you want to post to the same page you can either use 'action=index.php?page=3&var=10' or action=<?php echo $_SERVER['PHP_SELF'] ?>
You can check at the beginning of the page if something submitted and then redirect to whatever you want with header('Location: http://www.example.com/'); More about header function http://php.net/manual/en/function.header.php
Yeah, the solution is quite simple (even if not really SEO friendly):
<?php
header("Location: http://mysite.com")
?>
just for information...why do you need it?
use parse_str to get the query string as an associative array that is easy to modify. Then use http_build_query to convert the associative array into a query string.
$queryString = $s['QUERY_STRING'];
$params = array();
parse_str($queryString, $params);
//change $params as needed
$queryString = http_build_query($params);
if ($queryString) {
$queryString = '?'.$queryString;
}
return preg_replace("/\\?.*/s","",$s['REQUEST_URI']).$queryString;
preg_replace("/\\?.*/s","",$s['REQUEST_URI']) removes the original query string allowing you to replace it.
Does this work for you?
header('Location:/');
mod_rewrite cannot affect what's displayed in the user's browser address bar, UNLESS the rewrite does an externally visible redirect. Otherwise it only rewriting things within the webserver, and that's invisible to the user.
If you want to affect the user's address bar, you'll have to do a redirect via header('Location: ...') after the form's finished processing.

need help in particular php job, dont know how to do it

I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.

Categories