这可以用页面切换吗?
Is this possible with a pageswitch?
通常我会使用 url 参数来切换页面,
假设我有这个开关
所以 p 是页面
if(isset($_GET['p']){
$page = $_GET['p'];
if(file_exists('includes/' . $page . '.php')){
// include the page
} else {
// include error page
}
} else {
// include homepage
}
我的 url 看起来像这样:
http://www.example.com?p=contact
是否可以像这样使用 url 进行页面切换:
http://www.example.com/contact
提到 url.
末尾缺少的 .php
谁能告诉我这个 php if else 语句是否是包含文件的正确方法?
是的,可以使用 http://www.example.com/contact 等页面进行页面切换。
但是您需要重写流量,因此如果您可以访问 .htacess 文件,您可以添加类似这样的内容
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.+)/?$ http://www.example.com?p= [NC,L,QSA]
这样用户可以导航到 /contact,它将重定向到 ?p=contact
如果您有很多页面,我可以建议您使用 switch 语句来检查您的 $_GET 吗?
你的案例中的 switch 语句可能如下所示...
if(isset($_GET['p'])){
$page = $_GET['p'];
switch($page){
case 'contact':
if(file_exists('includes/' . $page . '.php')){
// include the page
} else {
// include error page
}
break;
}
}else{
}
希望对您有所帮助
通常我会使用 url 参数来切换页面,
假设我有这个开关 所以 p 是页面
if(isset($_GET['p']){
$page = $_GET['p'];
if(file_exists('includes/' . $page . '.php')){
// include the page
} else {
// include error page
}
} else {
// include homepage
}
我的 url 看起来像这样:
http://www.example.com?p=contact
是否可以像这样使用 url 进行页面切换:
http://www.example.com/contact
提到 url.
末尾缺少的 .php谁能告诉我这个 php if else 语句是否是包含文件的正确方法?
是的,可以使用 http://www.example.com/contact 等页面进行页面切换。
但是您需要重写流量,因此如果您可以访问 .htacess 文件,您可以添加类似这样的内容
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.+)/?$ http://www.example.com?p= [NC,L,QSA]
这样用户可以导航到 /contact,它将重定向到 ?p=contact
如果您有很多页面,我可以建议您使用 switch 语句来检查您的 $_GET 吗?
你的案例中的 switch 语句可能如下所示...
if(isset($_GET['p'])){
$page = $_GET['p'];
switch($page){
case 'contact':
if(file_exists('includes/' . $page . '.php')){
// include the page
} else {
// include error page
}
break;
}
}else{
}
希望对您有所帮助