开发者

How can you use a url path as input to a script?

开发者 https://www.devze.com 2023-01-11 19:57 出处:网络
Say I am rolling out a CMS. Each user gets their own page at domain.com/username/index.php. At the moment I\'m considering having a new account create a folder at said location and make an index file

Say I am rolling out a CMS. Each user gets their own page at domain.com/username/index.php. At the moment I'm considering having a new account create a folder at said location and make an index file like so:

<?php
$username=blahblah;
require("../indextemplate.php");
?>

Which would work fine. Everyone gets their own custom pages and such. What seems stupid to me is that I have to make identical folders and files for every unique user. Is there a way instead to catch people using a domain.com/username. And send them directly to indextemplate.php?

As I'm t开发者_如何学运维yping this I realized I might be able to use the 'that page does not exist' redirect that apache has in combination with $_SERVER["REQUEST_URI"] (not sure if the apache redirect will mess w/ the url) to go to the correct page.

I guess I'm asking then which approach is better (creating millions of folders/pages was the solution i found googling but it really seems worse). Is their a horrible downside to the second option? Or a third way perhaps.


Use a .htaccess file if you want to use these url redirects.

Basically the .htaccess could tell the server to treat example.com/user/Idiomatic as example.com/index.php?q=user&name=Idiomatic, then it's all php/mysql to load the data by these $_GET variables. This is used by many CMS'es like Drupal.

http://en.wikipedia.org/wiki/Rewrite_engine

Creating a new file each time is pointless, way to many files would be created.


May be,

<?php
$username=blahblah;
require("../indextemplate.php?username=".$username);
?>

And then, I guess you need use $_GET['username'] in your indextemplate script. Tray it!


Have a look at mod_rewrite.

For example, you have user.php:

$username = $_GET['username'];
require("../indextemplate.php");

Rewrite could be configured something like this:

RewriteEngine On
RewriteRule ^(\w+)/?$ user.php?username=$1 [L]
0

精彩评论

暂无评论...
验证码 换一张
取 消