更改PHP";“必需”;参数破坏站点结构

更改PHP";“必需”;参数破坏站点结构,php,Php,我的任务是对一个网站进行一些修改。特别是更改键入站点URL时用户重定向到的主页。现在,用户立即被转移到“login”页面,如我的“index.php”中所述: index.php <?php session_start(); $all_url = explode('?', $_SERVER['REQUEST_URI']); $url = explode('/', $all_url[0]); //Homepage if (empty($url[1])) { $url[1] = 'home

我的任务是对一个网站进行一些修改。特别是更改键入站点URL时用户重定向到的主页。现在,用户立即被转移到“login”页面,如我的“index.php”中所述:

index.php

<?php

session_start();
$all_url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('/', $all_url[0]);

//Homepage
if (empty($url[1])) {
$url[1] = 'home';
}

//DB connect
$config = array(
'host' => 'localhost', //CHANGE THIS, DB SERVER
'user' => 'xxx', //CHANGE THIS, DB USER
'password' => 'xxx', //CHANGE THIS, DB PASSWORD
'database' => 'xxx' //CHANGE THIS, DB NAME
);
$DB = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['database'], $config['user'], $config['password']);
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$DB->exec("SET names utf8");

if (file_exists('controller/' . $url[1] . '.php')) {

$gtpl = 'main';
if (empty($_SESSION['uid']) && $url[1]!='register') {
    //login
    require 'controller/login.php';

}else{
    require 'controller/' . $url[1] . '.php';
}

require 'view/' . $gtpl . '.php';
} else {
echo 'ERROR - File not found!!!';
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">

    <link rel="stylesheet" href="/css/bootstrap.css">
    <link rel="stylesheet" href="/css/bootstrap-theme.css">
    <link rel="stylesheet" href="/css/style.css">
    <link rel="stylesheet" href="/css/jquery-ui-1.10.3.css">
    <link rel="stylesheet" href="/css/colpick.css">
    <!--[if lt IE 9]>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
    <![endif]-->
</head>
<body>

    <div class="container">

        <?php require 'view/frontend/'.$tpl.'.php';?>

    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="/js/jquery-1.10.2.min.js"><\/script>')</script>
    <script src="/js/jquery-ui-1.10.3.js"></script>
    <script src="/js/jquery.migrate.js"></script>
    <script src="/js/farbtastic.js"></script>
    <script src="/js/crop.img.js"></script>
    <script src="/js/colpick.js"></script>
    <script src="/js/dropdown.js"></script>
    <script src="/js/jquery.numeric.js"></script>
    <script src="/js/init.js"></script>
</body>

我想让它,以便用户被转移到“家”而不是“登录”,并使用登录表单在网站稍后。问题是-当我更改require'controller/login.php'时;需要“controller/home.php”;,它会将我重定向到该页面,但我无法移动到网站中的任何其他地址。应该进行哪些更改才能使其正常工作?我还提供了“main.php”,以防万一

main.php

<?php

session_start();
$all_url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('/', $all_url[0]);

//Homepage
if (empty($url[1])) {
$url[1] = 'home';
}

//DB connect
$config = array(
'host' => 'localhost', //CHANGE THIS, DB SERVER
'user' => 'xxx', //CHANGE THIS, DB USER
'password' => 'xxx', //CHANGE THIS, DB PASSWORD
'database' => 'xxx' //CHANGE THIS, DB NAME
);
$DB = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['database'], $config['user'], $config['password']);
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$DB->exec("SET names utf8");

if (file_exists('controller/' . $url[1] . '.php')) {

$gtpl = 'main';
if (empty($_SESSION['uid']) && $url[1]!='register') {
    //login
    require 'controller/login.php';

}else{
    require 'controller/' . $url[1] . '.php';
}

require 'view/' . $gtpl . '.php';
} else {
echo 'ERROR - File not found!!!';
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">

    <link rel="stylesheet" href="/css/bootstrap.css">
    <link rel="stylesheet" href="/css/bootstrap-theme.css">
    <link rel="stylesheet" href="/css/style.css">
    <link rel="stylesheet" href="/css/jquery-ui-1.10.3.css">
    <link rel="stylesheet" href="/css/colpick.css">
    <!--[if lt IE 9]>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
    <![endif]-->
</head>
<body>

    <div class="container">

        <?php require 'view/frontend/'.$tpl.'.php';?>

    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="/js/jquery-1.10.2.min.js"><\/script>')</script>
    <script src="/js/jquery-ui-1.10.3.js"></script>
    <script src="/js/jquery.migrate.js"></script>
    <script src="/js/farbtastic.js"></script>
    <script src="/js/crop.img.js"></script>
    <script src="/js/colpick.js"></script>
    <script src="/js/dropdown.js"></script>
    <script src="/js/jquery.numeric.js"></script>
    <script src="/js/init.js"></script>
</body>

window.jQuery | | document.write(“”)

你可以换成:

$notrestricted = array('register','home'); 

if (empty($_SESSION['uid']) && !in_array($url[1], $notrestricted)) {
    //login
    require 'controller/login.php';
}

问题在于从未设置会话uid属性。这可能是由登录控制器完成的

这意味着永远不会调用代码的else子句。 解决方案:使用另一个标志,并在未设置时将用户重定向到主页。将用户重定向到主页后设置标志

if (!isset($_SESSION['firstVisit']) && $url[1]!='home') {
   $_SESSION['firstVisit'] = false; //ensures we never enter this clause again
   require 'controller/home.php';

}else{
   require 'controller/' . $url[1] . '.php';
}
不过,如果您希望某些页面仅在用户登录时才可访问,则应如下处理:

$restricted = array('page1','page2');  // restricted pages behind login

if (!isset($_SESSION['firstVisit']) && $url[1]!='home') {
   $_SESSION['firstVisit'] = false; //ensures we never enter this clause again
   require 'controller/home.php';
}else if (empty($_SESSION['uid']) && in_array($url[1], $restricted)) {
   //not logged in, but trying to access restricted page.
   require 'controller/login.php';
}else{
   require 'controller/' . $url[1] . '.php';
}

“我不能移动到站点中的任何其他地址”是什么意思?脚本将始终重定向到home.php,因为用户未登录。这是一个逻辑问题,而不是脚本问题。尝试以来宾身份或其他方式自动登录。@HüseyinBABAL这是一个商店网站,有“购物车”功能、注册等。这些功能不起作用。@user2315641,因为如果用户未登录,您会将其重定向到主页。您只需将其应用于未更改任何内容的受限页面。我不希望用户在登录时被转移到“主页”,如果没有,则被转移到“登录”。我想完全忽略“登录”选项。