choche7
choche7

Reputation: 59

PHP append variable to url in address bar

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?

Upvotes: 2

Views: 1870

Answers (3)

donquixote
donquixote

Reputation: 420

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

Upvotes: 1

Buildersrejected
Buildersrejected

Reputation: 167

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.

Upvotes: 1

TheAlexLichter
TheAlexLichter

Reputation: 7289

Try to use a redirection with headers, like

header("Location: http://communityimpact.com/discussions/central-austin");

Upvotes: 1

Related Questions