douchujian8124 2011-03-07 15:31 采纳率: 100%
浏览 5
已采纳

在包含?的网站上制作php登录脚本的干净方法?

Basically I have an index.php file with navigation in form of includes only.


  • My script can check whether a user exists by checking a database.

  • It can also log you in if the check was successful.

My problem is redirects! Do I really have to use echo "<meta http-equiv=refresh content=\"1; URL=index.php\">";?

And I also have to make tons of if-statements to cover the entire possibilities of a users actions (have to take evil people into account).

Headers are already ruled out because I use includes and requires.

I'm on the brinck of switching to ASP.NET or something rediculous... This scripting is making me mad :P

  • 写回答

5条回答 默认 最新

  • douhoushou8385 2011-03-07 16:16
    关注

    Sounds like you either need to use an existing content management system or framework or learn to code in a modular drop-in fashion. Coding it for modularity would mean that the included script can be left out and the page still function as usual.

    I'll post an example in a bit.

    Just looked at your code. The index page is insecure. Do not do it like that. I could do something nasty like index.php?p=../../../whateverfile and try to include it from outside of /inc/. You need some sort of protection against user input. Something like an array specifying valid files to include to check against, or a db table containing valid files to include that it can check against.

    edit Also, never ever store the password in a cookie. You should generate a unique key or something for the login and store it and check against it instead of the password.

    Here's what you'd want to do: Split the login file up into checking logic and presentation. Once you do that it means that the checking logic can be included anywhere on the page, while the form itself can also be placed anywhere.

    Here's a little example:

    loginCheck.php

    if(isset($_POST['login']))
    {
    
         if(!$_POST['username'] || strlen($_POST['username']) <= 3 || strlen($_POST['username']) >= 20)  //Check user input for validity
         {
              $loginerror['username'] = "Username is required.  Must be between 3 and 20 characters long.";
         }
         if(!$_POST['password'])
         {
              $loginerror['password'] = "Password is required.";
         }
    
    
         if(count($loginerror) == 0)
         {     
              $username = mysql_real_escape_string(trim($_POST['username'])); //Do whatever to the user input
              $password = mysql_reql_escape_string(trim($_POST['password']));
    
              $sql = mysql_query("SELECT `username`,`password`,`etc` FROM `users` WHERE `username` = '$username' AND `password` = '$password' LIMIT 1");  //Select both at the same time
    
              if(mysql_num_rows($sql) == 0)
              {
                   $loginerror['login'] = "Username or Password incorrect or does not exist.";  //It's smart not to let people know which they got wrong.
              }
              else
              {
                   $_SESSION['username'] = $username;
                   $_SESSION['loggedin'] = true;
                   $loginmessage = 'Welcome ' . $username. ', you are successfully logged in';
              }
          }
     }
    

    loginForm.php

    function dispError($name,&$errors)
    {
         if(isset($errors[$name]))
         {
              return '<span class="error">' . $errors[$name] . '</span>';
         }
         return '';
    }
    
    if(isset($loginmessage))
    {
         echo $loginmessage;
    }
    elseif(isset($_SESSION['username']) && isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true)
    {
         echo 'Welcome, ' . $_SESSION['username'];
    }
    else
    {
         if(!isset($loginerror) || !is_array($loginerror))
         {
              $loginerror = array();  //Gotta make sure it exists for the next part if it hasn't been set.
         }
         echo dispError('login',$loginerror);
         echo '<form method="post" action="">';
         echo '<input name="username" placeholder="Username..." type="text" maxlength="15" />' . dispError('username',$loginerror) . '<br /><br />';
         echo '<input name="password" placeholder="Password..." type="password" maxlength="20" />' . dispError('password',$loginerror) . '<br /><br />';
         echo '<input name="login" type="submit" value="Login" style="width:100px;">';
         echo '</form>';
    }
    

    index.php

    if(isset($_POST['login']))
    {
         require_once("loginCheck.php");
    }
    
    //various other includes and requires
    
    require_once("loginForm.php");
    

    This way there's also no reason to redirect away from the login form/sign in page, as the checks can be easily included inline and both the form and the check can be included on any and all pages dynamically just by dropping it in where necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计