Take Unique id and use as url for page
I am building a website for sharing links and pictures for php experience. Currently, each post has a page where users can comment on it. E开发者_如何转开发very post also has an id that is stored in a database with it. What would be the easiest way to make this id the url of the post? For example: For the question that has an id of 4a3cd5, I would want the url for that question to be post.com/posts.php/4a3cd5. thanks for your answers.
In the HTAccess page (.htaccess
), write the following code:-
Options +FollowSymLinks
RewriteBase /
RewriteRule ^posts/(.*)/$ /posts.php?id=$1
So now, in the Address Space, if you write http://localhost/posts/4a3cd5
, the user will be shown a page corresponding to the post ID of 4a3cd5
. Internally, the URL which will get processed is this one http://localhost/posts.php?id=4a3cd5
. This whole technique is being done by HTAccess, and this way of showing URLs to users is called SEF URLs.
More on the HTAccess tips & tricks can be found here.
Now in the page "posts.php
", you can write all the logic which you want using the PHP GET Superglobal Array "$_GET['id']
".
NP: A special note - please try to avoid this type of coding. Instead try using any of the available standard PHP MVC Frameworks.
That would involve a lot, maybe you should just have URL's like /posts.php/?post=4a3cd5
.
Overall this would be much more practical. You can just GET
the post variable and connect to your SQL database.
精彩评论