dsdv76767671 2013-04-17 12:00
浏览 22

如何正确处理重定向和包含页面?

I am writting a small project, a page where non registered users can access only few pages, registered ones to other few, and some of the registered ones to others. It's for a game, so only these playing a 'match' would have access to the latter. Furthermore, there will be some pages for us, the admins (secret pages). This is the structure:

$Public_Path = '/public_html/public/';
$Private_Path = '/public_html/private/';
$Secret_Path = '/public_html/secret/';

I pass everything through /public_html/index.php and include the files from there. I thought it would be better to have all the include and redirect in one place instead of scattered around at the beginning of each file. The thing is, code starts to get messy in here, and there's still many more options to add. Is there a schema that simplifies it? This is how my code looks right now:

// Some extra code goes here

// If an unregistered user tries to go anywhere he shouldn't
if (!file_exists($Public_Path . $Url . '/index.php') && empty($User))
  header ('Location: /login/');

// PUBLIC. Load the public pages
if (file_exists($Public_Path . $Url . '/index.php'))
  include $Public_Path . $Url . '/index.php';

// SECRET. Only for admin
else if (file_exists($Secret_Path . $Url . '/index.php') && in_array($User->email, $Admins))
  include $Secret_Path . $Url . '/index.php';

// PRIVATE. Load the template and include private pages
else if (file_exists($Private_Path . $Url . '/index.php'))
  {
  if ($UrlArray[0] != 'games' && $User->game == 0)
    header ('Location: /games/');

  if ($UrlArray[1] == 'save')
    include $Private_Path . $Url . '/index.php';
  else
    {
    $Page = $Private_Path . $Url . '/index.php';
    include $Include_Path . 'template.php';
    }
  }

// 404. Nor public nor private nor secret
else
  header ('Location: /error/404');

Note: I know the limitation of only being able to access index.php with this, I imposed that myself.

My question is, how can I order this code in some fashion that allows me to add much more functionality but increasing only a little the complexity? How to reduce the latter?

  • 写回答

1条回答 默认 最新

  • dongzhimin2231 2013-04-17 18:59
    关注

    I would consider a couple of options:

    Add a session variable (say, $_SESSION['userPath']), and when the user logs in, take them to their authorized path:

    $path = (isset($_SESSION['userPath']))?$_SESSION['userPath']:FALSE;
    
    if($path){
      include $path . $Url . '/index.php';
    } else {
      header ('Location: /login/');
    }
    

    You format works well for expansion, but it may be semantically easier to read if you change it to a switch statement. This would require you to get it down to one variable (such as the $_SESSION['userPath']) variable mentioned above.

    switch ($path){
      case "Public_Path":
        header ('Location: /login/');
        break;
      case "Private_Path":
         if ($UrlArray[0] != 'games' && $User->game == 0){
          header ('Location: /games/');
         }
         if ($UrlArray[1] == 'save'){
          include $Private_Path . $Url . '/index.php';
         } else {
          $Page = $Private_Path . $Url . '/index.php';
          include $Include_Path . 'template.php';
         }
        break;
      case "Secret_Path":
        if (in_array($User->email, $Admins){
          include $path . $Url . '/index.php';
        }
        break;
      case "New_Path":
        include $path . $Url . '/index.php';
        break;
    
       // ...
    
      case default:
        header ('Location: /error/404');
    }
    

    Finally, if you use an AJAX solution, rather than redirect, the page would reload to the viewer appropriate location. In this case, no redirect would be necessary at all, but rather, just loading the required elements at the time of need.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法