2

Are there ways to pass variables in a URL similarly to GET data? For example, with slashes?

I currently have a single .php file which reloads a div with different content using javascript to navigate pages, but as you can imagine, the address in the address bar stays the same.

I want users to be able to link to different pages, but that isn't possible conventionally if there is only one file being viewed.

Amit Verma
  • 38,175
  • 19
  • 80
  • 104
bronado
  • 83
  • 1
  • 2
  • 4
  • 2
    You can get the value of `$_SERVER['QUERY_STRING']` and parse it any way that you want. – Orangepill Jul 15 '13 at 15:06
  • Provide some URL examples please of what you want – Robert DeBoer Jul 15 '13 at 15:06
  • Please give an example of the desired URLs. Sounds like you're asking about `PATH_INFO` or remotely [hashbang](http://stackoverflow.com/questions/3009380/whats-the-shebang-hashbang-in-facebook-and-new-twitter-urls-for) identifiers. – mario Jul 15 '13 at 15:07
  • 'wesbtie.com/user/john' And then I can manipulate 'user' and 'john' like GET data. – bronado Jul 15 '13 at 15:08
  • Possible duplicate of http://stackoverflow.com/questions/15118047/php-url-explode – André Dion Jul 15 '13 at 18:46

6 Answers6

0

You're probably going to want to use something along the lines of Apache's mod_rewrite functionality.

This page has a nice example http://www.dynamicdrive.com/forums/showthread.php?51923-Pretty-URLs-with-basic-mod_rewrite-and-powerful-options-in-PHP

Matthew G
  • 990
  • 2
  • 13
  • 19
0

Try using:

$_SERVER['QUERY_STRING']; // Or
$_SERVER['PHP_SELF'];

If that doesn't help, post an example of what kind of URL you are trying to accomplish.

virtuexru
  • 798
  • 1
  • 5
  • 16
  • neither will work. first one is dependent on a query, which he doesn't want and unless the script name can change dynamically like /user/(anything) `PHP_SELF` won't work either. – Jay Harris Jul 15 '13 at 15:35
0

Something like this might do the trick;

place this in /yourdir/

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ yourindexfile.php?string=$1 [QSA,L]

All requests will be sent to yourindexfile.php via the URL. So http://localhost/yourdir/levelone becomes yourindexfile.php?string=levelone.

You'll be able to break down the string like so;

$query= explode('/', rtrim($_GET['string'], '/'));
0

the technology your looking for is .htaccess. technically this isn't possible, so you'll have to hack your mod rewrite to accomplish this.

  RewriteRule On +FollowSymLinks
  RewriteCond %{SCRIPT_FILENAME} !-d  
  RewriteCond %{SCRIPT_FILENAME} !-f 
  RewriteRule ^(user)/([^\.]+)$ ./index.html?tab=user&name=$2

add this to your .htaccess page in your top directory. you'll have to alter your website structure a little bit. assuming that index.html is your index. this is a backwards rewrite so if one was to go to the page with the query string it won't redirect them to the former page and if one went to the page without the query string it will work like GET data still.

you GET this data with your php file using $_GET['tab'] and $_GET['name']

Jay Harris
  • 4,033
  • 15
  • 21
0

I think the Symfony Routing Component is what you need ;) Usable as a standalone component it powers your routing on steroids.

Emii Khaos
  • 9,628
  • 3
  • 31
  • 55
0

I'm doing it like this (in my like framework, which is a fork of the JREAM framework):

RewriteEngine On

#When using the script within a subfolder, put this path here, like /mysubfolder/
RewriteBase /mysubfolder/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Then split the different URL segments:

$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url_array = explode('/', $url);

Now $url_array[0] usually defines your controller, $url_array[1] defines your action, $url_array[2] is the first paramter, $url_array[3] the second one etc...

Sliq
  • 14,005
  • 24
  • 99
  • 137