0

Perhaps I'm using the wrong search terms to try to find this online, but I am trying to accomplish the task of passing a variable in a URL path, with using an identifier.

For example, here is my current URL: http://www.myurl.com/test/index.php?name=bob

On my index.php page, I would set something along the lines of $name = $_GET['name']; and have no issue using this variable.

My goal, however, would be to use the URL: http://www.myurl/test/bob/ and still be able to receive "bob" as the name variable in my script.

Is this possible, hypothetically? Thank you!

ZbadhabitZ
  • 2,753
  • 1
  • 25
  • 45

2 Answers2

0

put in your htaccess a mod_rewrite statement like

RewriteRule ^test\/([a-z]+)\/?$ index.php?name=$1
Bernhard
  • 1,852
  • 11
  • 19
0

One of the easiest ways to do this (and also a bit more secure) is instead of using a GET statement, using a session variable. You could change the url to be whatever you like using mod_rewrite as you have suggested, however you can still access the variable without anything special.

For instance, you just start your session like so

session_start();

then set your session variable, like so (assuming you have already defined $name):

$_SESSION['name']  = $name;

and then on the page where you'd want to get name, put session_start(); at the top of the page, and then, instead of $_GET['name'] just call the variable as $_SESSION['name'] instead.

This way you don't really need to worry about using the URL for passing the variable from one page to another. It won't be affected by rewriting.

Of course, your other option, if you wish to continue using it as a GET variable is this: https://stackoverflow.com/a/8228851/3044080

Community
  • 1
  • 1
nomistic
  • 2,902
  • 4
  • 20
  • 36