1

I would like to be able to use jQuery $.post with url segments as follows:

www.mysite.com/stats/user1

www.mysite.com/stats is the landing page, with this code on it:

var user = //get user1 from the url;

$.post('stats.php', {username : user}, function(results){...});

The username should be posted to a PHP backend which then does some database querying.

Is this possible? I might be able to use .htaccess to redirect users from stats/user1 to stats/, but I don't have much experience in this area.

Many thanks in advance for your help.


EDIT

In response to Loopo's answer:

I can use .htaccess to rewrite incoming URLs as follows:

RewriteRule ^/stats/(.*)$ stats.php?username=$1 [L].

This would allow me to enter a URL such as mysite.com/stats/user123, which the server would interpret as mysite.com/stats?username=user123

In stats.php can I then use $user = $_GET['username']?

ms813
  • 231
  • 2
  • 17

1 Answers1

1

It seems you are mixing some concepts up.

If you want to pass data to a webserver, you can do it in two ways; POST or GET. (There is also PUT and DELETE but we'll ignore that here.)

If you are using a GET request, the data is in the URL, normally in a format like

mysite.com/mypage.php?param1=value1&param2=value2....

the slashes normally act as a path separator to tell the webserver where to look for the resource that answers the request.

mysite.com/myapp/myfolder/resources/logo.png

would tell the server where to find and then send the file logo.png to the client.

If you want to have parameters in the path, you can also use redirection (.htaccess) to have virtual resources.

as in your example.

www.mysite.com/stats/user1

there is no stats folder with a script for every user.

You'll have to tell your webserver when someone asks for some path that looks like
/stats/<something>
the request should be served by some script (probably 'stats.php') and the parameters that are passed to the script will be <something>

in your .htaccess this might look like:

 RewriteRule ^/stats/(.*)$ stats.php/$1 [L]

In a POST request the parameters are not visible in the url.

So your stats.php would have to be called without the username in the URL, but instead in the POST variables, i.e. in your case POSTing to stats.php/user1 and then including the username again in the POST variables is redundant.

So your stats.php could deal with a POST request by reading in the parameters and creating/updating a new user with the values provided in the POST parameters, while dealing with GET requests by returning the user's stats as they are now.

What I am describing is REST, see also

Community
  • 1
  • 1
Loopo
  • 2,045
  • 2
  • 24
  • 42
  • Thanks for your detailed answer - I've added an edit to my question to try to clarify – ms813 Jul 26 '14 at 13:14
  • Looks like you're on the right track ... you can also use the $_SERVER (http://php.net/manual/en/reserved.variables.server.php) array to get the user, particularly $_SERVER['REQUEST_URI'] – Loopo Jul 27 '14 at 08:25