PHP替换页面参数
我对 PHP 和 MySQL 很陌生,我有一些问题。
首先,我有一个像这样的菜单:home.php、about.php等。当有人访问home.php?page=1&opt=2&etc时,我希望URL出现domain.com/home/1/ 2/等。有什么办法可以实现这一点吗?
其次,将页面内容保存在数据库中,然后在请求页面时从该数据库显示它是一个很好的做法吗?我的意思是,如果链接是 home.php?page=1&opt=2&etc,我必须使用这些参数进行 MySQL 数据库询问,如果找到,我必须显示该字段(其中包含 html/php 代码) )。这是正确的吗?或者我应该采取什么方法?
谢谢!
I am very new to PHP and MySQL and I have some questions.
First, I have a menu like this: home.php, about.php, etc. When someone access home.php?page=1&opt=2&etc, I want the URL to appear domain.com/home/1/2/etc . Is there any way to achieve this?
Second, it is a good practice to save the pages content in a database and then display it from that database when the page is requested? I mean if the link is home.php?page=1&opt=2&etc, I have to do a MySQL database interrogation with those parameters, and if it is found I have to display the field (which contains html/php code). It is correct? Or what approach should I have?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如斯拉普内尔上校所说,情况恰恰相反,但你应该做点别的事情。
这篇文章可能会对您有所帮助。
http://www.addedbytes.com/for-beginners/url- rewriting-for-beginners/
或者,假设您使用的是 Apache,您可以执行以下操作:
在您的 .htaccess 中:
这将返回所请求的资源(如果存在)或转换您的网址:
www.domain.com/1/2
进入
www.domain.com/index.php?args=/1/2
PHP 中的某处:
您必须解析一个参数:$_GET["args"]
从示例中它将包含“/1/2”。
请注意,所有请求都将通过index.php 输入。
我建议您仔细设计 URL 并编写尽可能通用的解析器。例如:
/x/y 是否始终意味着:page=x opt=y 或者有时意味着发票=x line=y?
也许您可以考虑在请求中添加更多信息:
www.domain.com/pages/1/2 将清楚地引用页面。
当然,如果您只发送页面,第一个选项就可以了。
As Col. Shrapnel said, it's the other way around, but you should do something else.
This article may help you.
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
Or, assuming you are using Apache, you could do something like this:
In your .htaccess:
This will return the requested resource if it exists or transform your URL:
www.domain.com/1/2
into
www.domain.com/index.php?args=/1/2
Somewhere in your PHP:
You will have to parse the one parameter: $_GET["args"]
Which from the example it would contain "/1/2"
Note that all requests are going to enter through index.php.
I would suggest you to design your URL's carefully and write a parser as general as possible. For instance:
Will /x/y always mean: page=x opt=y or sometimes it will mean for example invoice=x line=y?
Maybe you could consider to add some more info to the request:
www.domain.com/pages/1/2 Would clearly refer to pages.
Of course that, if you only will dispatch pages, the first option will work ok.
编辑。
好吧,根据您的评论,这是另一个答案。
不要弄乱网址。暂时忘记它吧。毕竟只是一个装饰品。它会让您感到困惑并浪费太多时间,而它的重要性远不如您的主要目标 - 网站本身。去学习一些教程(我相信 Sitepoint.com 有一个)并学习如何构建动态 PHP+mysql 驱动的网站。这不是一个两段答案可以回答的主题。事实上,人们会参加大约一年的课程来获得基础知识。你要求太多了,就好像网络开发就像一个愚蠢的游戏一样。它不是。
这样做是没有意义的。
唯一明智的是相反的:有人访问domain.com/home/1/2/etc,但处理的真实地址是home.php?page=1&opt=2&etc
“是”字段。
你走在正确的轨道上。这正是世界上每个网站的运作方式。
是的,但有一个更正:没有 PHP。仅 HTML
EDIT.
Well, based on your comments, here is another answer.
Do not mess with urls. Just forget it for a while. After all it's just a decoration. It will confuse you and waste too much time, while it's way less important than your main goal - site itself. Go for some tutorial (I believe Sitepoint.com have one) and learn how to build dymamic PHP+mysql driven site. It is not the topic that can be answered in a 2-paragraph answer. In fact, people go for classes for about a year to get the basics. You're asking too much, as though web-development is like a silly game. It is not.
There is no sense in that.
The only thing is sensible is opposite one: someone access domain.com/home/1/2/etc but real address processed is home.php?page=1&opt=2&etc
Yes.
You're on the right track. That's exactly how every site in the world works.
Yes, but with one correction: without PHP. HTML only