PHP登录验证

时间:2009-11-23 12:24:55

标签: php authentication

<?php
 session_start();

 if($_GET["log"] == "out") {
 session_unset();
 session_destroy();
 echo "done";
 }
 $username = $_POST["user_login"];
 $password = $_POST["user_pass"];

 if($_SESSION['LoggedIn']) {
 $_SESSION['LoggedIn'] = 1;
 $_SESSION['user_login'] = $username;
 $_SESSION['user_pass'] = $password;
 echo $_SESSION['user_login']."<br />".$_SESSION['user_pass'];
 $ftp_server = "127.0.0.1";
 $gName = $_GET["name"];

 require_once "ftp.class.php";
 $ftp =& new FTP();

 if ($ftp->connect($ftp_server)) {
  if ($ftp->login($_SESSION['user_login'],$_SESSION['user_pass'])) {
   echo "\n".$ftp->sysType() . "\n";
   echo $ftp->pwd() . "\n";
   if($gName)
   $ftp->chdir("$gName");
   else
   $ftp->chdir("Downloads");
   print_nice($ftp->nlist());
   echo "\n";
  } else {
   echo "login failed: ";
   print_r($ftp->error_no);
   print_r($ftp->error_msg);
  }
  $ftp->disconnect();
  print_r($ftp->lastLines);
 } else {
  echo "connection failed: ";
  print_r($ftp->error_no);
  print_r($ftp->error_msg);
 }
}
else
{
?>
<form method="POST" style="margin: 20px 10px; padding: 15px 25px 25px 20px; border: 1px solid #EEE8E1; background: #FAF7F5;"> 
    <p>Login Form</p>
   <?php echo $_SESSION['user_login']."<br />".$_SESSION['user_pass']; ?>
    <p> 
     <label for="name">Username</label><br />
     <input id="name" name="user_login" value="" type="text" tabindex="97" />
    </p>

    <p>
     <label for="email">Password</label><br />
     <input id="email" name="user_pass" value="" type="password" tabindex="98" />
    </p>

    <p><input type="checkbox" value="1" name="rmb"> Remember Me <br /><a href="#">Forgot Password</a><br /><a href="index.php?page=register">Don't have an Account?</a></p>

    <p class="no-border">
    <input class="button" name="submit" type="submit" value="Submit" tabindex="14" />
           <input class="button" type="reset" value="Reset" tabindex="15" /> 
    </p>
<?php
}
?>

7 个答案:

答案 0 :(得分:1)

也许您可以先检查是否登录:

if(isset($_SESSION['LoggedIn']) &&$_SESSION['LoggedIn']==1)
{
   // proceed to do the login stuff
}
else if (isset($_POST['submit'])) 
{
    $username = $_POST["user_login"];
    $password = $_POST["user_pass"];

    $_SESSION['LoggedIn'] = 1;
    $_SESSION['user_login'] = $username;
    $_SESSION['user_pass'] = $password;
    // some coding about ftp login
}
else
{
}

答案 1 :(得分:1)

尝试

if ((isset($_POST['submit'])) OR ($_SESSION['LoggedIn'] == 1)) {

  // Logged in stuff...

}

答案 2 :(得分:0)

您是否添加了用户是否已登录的检查?

if (isset($_POST['submit']) || (isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'])) 

答案 3 :(得分:0)

改变第一行:

if (isset($_POST['submit']) || (isset($_SESSION['user_login']) && $_SESSION['user_login'] != '') 
{

答案 4 :(得分:0)

我不使用会话,但您可以像这样使用

if (isset($_POST['submit']))  {
     //....
}

if($_SESSION['LoggedIn']) {
    //actual coding
}

答案 5 :(得分:0)

我倾向于检查是否存在已登录的会话,然后重定向到用户的主要内容(即.page.php),或者如果您需要登录则显示登录框,我有一个类似的事情(仅重定向到login.php或显示内容)所有其他登录需要页面作为包含,以便它不断检查您是否打算登录并在需要时发送给您登录,停止你去page.php什么也看不见。

答案 6 :(得分:0)

你需要调用session_start();在每页的第一行。