2013-03-18 86 views
-1

我有一个CSS弹出窗口,显示一个登录表单。当我尝试登录时,只是重新加载页面,弹出窗口再次出现。CSS popup登录表单和PHP登录脚本

下面是打开弹出

<a href="#login_form" class="btn signInBtn">Sign In</a> 

链接这里是我的弹出登录表单的HTML。

<a href="#x" class="overlay" id="login_form"></a>  
    <div class="popup"> 
     <h2 class="modal-header">Sign In</h2> 
     <div class="modal-body"> 
      <form class="signIn-form"> 
       <div class="errorMessage alert alert-error" id="errorMsg"> 
        <h4>Whoops</h4> 
        <p> 
         <?php echo $errorMsg; ?> 
        </p> 
       </div> 
       <fieldset class="l-formMain"> 
        <ul> 
         <li> 
          <label class="applyForm-label" for="email">Email</label> 
          <input class="applyForm-input required" type="email" id="login_email" placeholder="[email protected]" tabindex="1" /> 
         </li> 
         <li> 
          <label class="applyForm-label" for="password">Password</label> 
          <input class="applyForm-input required" type="password" id="login_password" tabindex="2" /> 
          <span class="forgotPassLi"> 
           <a href="#forgotpass" class="v-secondary" style="position:relative; left:-150px;">Forgot your Password?</a> 
          </span> 
         </li> 
        </ul> 
       </fieldset> 
       <div class="modal-buttonHolder"> 
        <input type="submit" class="btn btn-large" id="login" value="Sign In" tabindex="3" /> 
       </div>      
      </form> 
     </div> 
     <a href="#close" class="modal-closeBtn">×</a>  
    </div> 

这是我的PHP:

if(isset($_POST['login'])){ 
    $login_email = $_POST['login_email']; 
    $login_password = $_POST['login_password']; 

    // error handling conditional checks go here 
    if ((!$login_email) || (!$login_password)) { 
    $errorMsg = 'Please fill in both fields'; 
    } else { // Error handling is complete so process the info if no errors 
    include 'scripts/connect_to_mysql.php'; // Connect to the database 
    $email = mysql_real_escape_string($login_email); // After we connect, we secure the string before adding to query  
    $pass = md5($login_password); // Add MD5 Hash to the password variable they supplied after filtering it  
    // Make the SQL query 
    $sql = "SELECT * FROM members WHERE email='$email' AND password='$password' AND email_activated='1'"; 
      $result = mysql_query($sql); 
    if($result){ 
     $login_check = mysql_num_rows($result); 
    } 
    // If login check number is greater than 0 (meaning they do exist and are activated) 
    if($login_check > 0){ 
      while($row = mysql_fetch_array($result)){ 

       $id = $row["ID"]; 
       $_SESSION['ID'] = $id; 
       // Create the idx session var 
       $_SESSION['idx'] = base64_encode("xxxxxxxxxxxxxxxxxxx$id"); 
       // Create session var for their username 
       $email = $row["email"]; 
       $_SESSION['email'] = $email; 
       $_SESSION['userId'] = $row["ID"]; 

       mysql_query("UPDATE members SET last_log_date=now() WHERE ID='$id' LIMIT 1"); 

      } // close while 
      // All good they are logged in, send them to homepage then exit script 
      if (isset($_SESSION["email"]) || count($_SESSION["email"]) > 0) { 
       header("Location: http://localhost/dashboard.php"); 
      }    
    } else { // Run this code if login_check is equal to 0 meaning they do not exist 
     $errorMsg = "Either the email or password (or both) are incorrect. Make sure that you've typed them correctly and try again"; 
    } 
    }// Close else after error checks 
    } 
?> 
+0

尝试设置表单的页面的动作,而不在URL中#login_form(我猜这个节目的时提交表单) – TommyBs 2013-03-18 12:54:56

+0

尽管你可以使用$ _REQUEST而不是$ _POST,你应该考虑使用预处理语句或者至少一个mysql_escape_string($ email),并且使用$ password来避免sql注入 – ITroubs 2013-03-18 12:56:15

+0

@ITroubs你说的对,它是这不是一个关于安全性的好例子。我编辑了剧本来迎合这一点。 – Janatan 2013-03-18 14:07:37

回答

0

你的形式不张贴到任何地方。所以默认情况下它会转到当前页面。

尝试增加的动作和方法属性的形式:

<form method="post" action="http://url.of.PHP.page/containing/login/code"> 
+0

嗨Husman,我已经添加了帖子和操作去仪表板。该页面直接进入URL,但它似乎没有通过PHP代码。 – Janatan 2013-03-18 14:19:02

+0

那是因为你的PHP代码正在寻找:if(isset($ _ POST ['login']))并且你的提交按钮没有名字属性,只是设置为'login'的id。要解决这个问题,请在提交按钮中添加name ='login'。 – Husman 2013-03-18 14:21:19