PHP:根据 URL 运行查询?
希望我能正确解释这一点...
我有一个搜索引擎脚本,它运行 PHP 并使用来自各个地方的 API。我希望能够根据 URL 生成查询结果 ($q)。
例如,如果用户访问domain.com/stackoverflow,我希望该站点自动生成一个运行搜索“stackoverflow”的查询。即 $q = stackoverflow.
我想这与 PHP 中的 header 函数相反,在 PHP 中,我不想将信息放入 URL 栏中,而是想取出信息。
我意识到可能需要对 .htaccess 进行一些编辑才能使其工作,但这可能吗?这也意味着能够处理与网站上可能已经存在的任何内容的冲突,例如,我需要定义保留domain.com/login,并且不对其运行查询搜索等。
Hopefully I can explain this properly...
I have a search engine script that runs off PHP and uses API's from various places. I would like to be able to generate query results ($q) based on the URL.
e.g. If a user goes to domain.com/stackoverflow I would like the site to automatically generate a query that runs a search for "stackoverflow". i.e. $q = stackoverflow.
I suppose this is the opposite of the header function in PHP, where instead of putting information into the URL bar I want to take information out.
I realise there may be some editing to .htaccess to make this work, but is it possible? It would also mean being able to handle conflicts with whatever might already sit on the site, e.g. I would need to define that domain.com/login is reserved and to not run a query search on that, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是通过
$_GET
数组完成的。假设这是您的网址:
http://www.website.com/?foo=bar
,那么$_GET['foo']
将是bar
代码>.如果你想做类似
domain.com/stackoverflow
的事情,你需要使用 htaccess 将该 url 重写为类似domain.com/?domain=stackoverflow
的东西,然后你可以通过$_GET['domain']
访问它。This is done through the
$_GET
array.Imagine this is your url:
http://www.website.com/?foo=bar
then$_GET['foo']
will bebar
.If you want to do something like
domain.com/stackoverflow
you will need to use htaccess to rewrite that url to something likedomain.com/?domain=stackoverflow
and then you can access it through$_GET['domain']
.如果我理解得很好,您可以使用函数 parse_url()。 这里有文档。
If I understood well, you could use the function parse_url(). Here you have the documentation.
有几种方法可以做你想做的事。
.htaccess
到ModRewrite< /code>
以某种方式将
example.com/?q=whatver
变为example.com/whatever
,然后使用 PHP 的$_GET
超全局。才能正常获取信息。parse_url()
函数,获取路径并使用它($parsed_url['path']
)。祝你好运 :)
There are several ways of doing what you want.
.htaccess
toModRewrite
it in a way so thatexample.com/?q=whatver
will becomeexample.com/whatever
, and then use PHP's$_GET
superglobal. to get the information normally.parse_url()
function, get the path and use it ($parsed_url['path']
).Good luck :)