2014-01-28 232 views
2

注销会议上,我想创建登录和注销会话创建登录和PHP和数据库

我在MySQL数据库表看上去就像命名为loginproc.php这

CREATE TABLE members ( 
id int(10) NOT NULL auto_increment, 
username varchar(20) NOT NULL, 
password varchar(20) NOT NULL, PRIMARY KEY (id)) 
ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; 

MySQL连接

<?php 

// Inialize session 
session_start(); 

// Include database connection settings 
$hostname = 'localhost';  // Your MySQL hostname. Usualy named as 'localhost',     so you're NOT necessary to change this even this script has already  online on the internet. 
$dbname = 'database'; // Your database name. 
$username = 'root';    // Your database username. 
$password = '';     // Your database password. If your database has no   password, leave it empty. 

// Let's connect to host 
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed,  perhaps the service is down!'); 
/Select the database 
mysql_select_db($dbname) or DIE('Database name is not available!'); 


// Retrieve username and password from database according to user's input 

$login = mysql_query("SELECT count(*) FROM members WHERE (username = '" .  mysql_real_escape_string($_POST['user']) . "') and (password = '" .  mysql_real_escape_string(md5($_POST['pass'])) . "')"); 
$result=mysql_fetch_array($login); 
// Check username and password match 

if (mysql_num_rows($result) == 1) { 
// Set username session variable 
$_SESSION['username'] = $_POST['user']; 

// Jump to secured page 
header('Location: securedpage.php'); 
} 
else { 
// Jump to login page 
header('Location:career.php'); 
} 

?> 

然后securedpage.php

创建
<?php 

// Inialize session 
session_start(); 

// Check, if username session is NOT set then this page will jump to login page 
if (!isset($_SESSION['user'])) { 
header('Location: career.php'); 
} 

?> 
<html> 

<head> 
<title>Secured Page</title> 
</head> 

<body> 

<p>This is secured page with session: <b><?php echo $_SESSION['username']; ?></b> 
<br>You can put your restricted information here.</p> 
<p><a href="logout.php">Logout</a></p> 

</body> 

</html> 

的index.php

<html> 
<head> 
</head> 
<body> 

<form action="loginproc.php" method="post"> 




       UserName:<input type="text" name="user" > 

    <p> &nbsp;</p> 
    Password:<input type="password" name="pass" > 
<p>&nbsp;</p> 

    <input type="submit" value=" Login Here " > 
    &nbsp; 
    <span class="style30">| New?</span> 
    <a href="signup.php"><span class="style32">Start Here</span> 

</form></body></html> 

,最后注销命名为logout.php页面

<?php 

    // Inialize session 
    session_start(); 

// Delete certain session 
    unset($_SESSION['username']); 
    // Delete all session variables 
    // session_destroy(); 

// Jump to login page 
header('Location: index.php'); 

    ?> 

现在我的问题是,当我输入用户名和密码,它会留只有在index.php,它不会进入另一个页面。请看看这段代码,并告诉我什么时候我做错了。

谢谢。

+0

的'index.php'代码看起来好像没什么问题。在index.php中还有没有其他的代码在这里粘贴?另外这行'loginproc.php'中的'/选择数据库'没有被正确评论,并且会被解释为一个语句。 –

+0

如果这是你的实际代码,该行'/选择database'缺少一个'/'所以尝试将其更改为'//选择database' - 这种类型的PHP评论需要2个''//它 –

+0

不'$ _SESSION [“用户”]'是'$ _SESSION [“用户名”]' –

回答

2

不要使用此行

$result=mysql_fetch_array($login); 

这将结果取到$结果作为数组,后来自己使用的是mysql_num_rows()函数(用于资源,即你的情况$登录)

你下面的代码

 $login = mysql_query("SELECT count(*) FROM members WHERE (username = '" .  mysql_real_escape_string($_POST['user']) . "') and (password = '" .  mysql_real_escape_string(md5($_POST['pass'])) . "')"); 

     // Check username and password match 

     if (mysql_num_rows($login) == 1) { 
     // Set username session variable 
     $_SESSION['username'] = $_POST['user']; 

     // Jump to secured page 
     header('Location: securedpage.php'); 
     } 
     else { 
     // Jump to login page 
     header('Location:career.php'); 
     } 
0

我看到两个问题:

  1. mysql_num_rowsresource类型为参数。您传递的结果是mysql_fetch_array,它可以是数组或FALSE。
  2. 您使用的是不推荐使用的mysql扩展名。对于新代码,您应该使用MySQLiPDO

要解决在点1 mysql_num_rows问题,请使用if (mysql_num_rows($login)) {

$login = mysql_query("SELECT count(*) FROM members WHERE (username = '" .  mysql_real_escape_string($_POST['user']) . "') and (password = '" .  mysql_real_escape_string(md5($_POST['pass'])) . "')"); 
$result=mysql_fetch_array($login); 
// Check username and password match 

if (mysql_num_rows($login) == 1) { 
+0

查询的var_dump(),但这样做仍然呈现像mysqli_num_rows错误()预计参数1是mysqli_result,在第24行给出的F:\ wamp \ www \ iwebtechnik \ loginproc.php中给出的布尔值 – user3242335

+0

这是因为您的查询失败。尝试在MySQL客户端中执行生成的查询。 – vee

1

我对你的问题的解决方案。你必须一点修改代码如下所述 -

<?php 

// Inialize会议

session_start(); 

//----***Use variabel to capture start time *****------ 

//检查,如果用户名会话也不会设置这个页面会跳转到登录页面

if (!isset($_SESSION['user'])) { 
header('Location: career.php'); 
} 

?> 

And in logout page add one entry as - 

<?php 

// Inialize会议

session_start(); 

//删除某些会话

unset($_SESSION['username']); 


//---****Use end time variable --------- 
// Subtract previous start time variable and end time variale 

//删除所有会话变量

// session_destroy(); ?

// Jump to login page 

    header('Location: index.php'); 

>

+0

mysqli_num_rows()期望在F中给出的参数1被mysqli_result,布尔:\瓦帕\ WWW \ iwebtechnik \ loginproc.php上线24 – user3242335

+0

问题是与查询..其不执行。请分享你的查询 – Partap

+0

“来自会员的WHERE SELECT COUNT(*)(用户名=“”。mysql_real_escape_string($ _ POST [ '用户'])。“')和(密码= '”。mysql_real_escape_string(MD5($ _ POST [' 通']))。“')”); – user3242335

1

您在这里有一个问题:

$login = mysql_query("SELECT count(*) FROM members WHERE (username = '". mysql_real_escape_string($_POST['user']) . "') and (password = '" .  mysql_real_escape_string(md5($_POST['pass'])) . "')"); 
$result=mysql_fetch_array($login); 
// Check username and password match 

if (mysql_num_rows($result) == 1) { 

您的查询将始终检索上mysql_num_rows($结果) 1行,因为它检索用户的计数与条件,如果没有一个匹配用户名和密码,查询检索

|count(*)| 
+--------+ 
|0  | 

,这是1行

0
  index.php 

      <!DOCTYPE html> 
      <html > 
       <head> 
       <meta charset="UTF-8"> 
       <title></title> 
       <link rel="stylesheet" href="css/style.css"> 
       <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'> 
       <link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png"> 
       <style> 
       .head{ 
       margin:auto; 
       margin-top: 40px; 
       margin-bottom: 40px; 
       width: 500px; 
       height: 50px; 
       text-align:center; 
      } 
       </style> 


       </head> 

       <body> 


        <div class="head"><h1> <span class="strong"></span></h1></div> 
       <div style="padding:0;" align="center" class="login-page"> 
        <img src="img/oms.png"><br><br> 
       <div class="form" > 


       <form class="login-form" name="frm" action="Logging.php" method="POST"> 
        <input type="text" placeholder="username" name="usrname"/> 
        <input type="password" placeholder="password" name="password"/> 
         <button type="submit" onclick="return logincheck()">login</button> 

        <p class="message"> Forgot Password <a href="forgotpass1.php">Click here</a></p> 
       </form> 
       </div> 
      </div> 
       <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

       <script type="text/javascript"> 

       function logincheck() 
        { 
        var x = document.frm.usrname.value; 
        var y = document.frm.password.value; 
        if(x =="" || x == null){ 
        alert("Enter the Username "); 
        return false; 
        } 
        else if(y=="" || y == null){ 
        alert("Enter the Password "); 
        return false; 
        }else{ 
        return true; 
        } 
        } 




       $('.message a').click(function(){ 
       $('form').animate({height: "toggle", opacity: "toggle"}, "slow"); 
      }); 
       </script> 




       </body> 
       <?php include 'footer1.php';?> 
      </html> 


      Logging.php 

      <!DOCTYPE html> 
      <html > 
       <head> 
       <meta charset="UTF-8"> 
       <title></title> 
       <link rel="stylesheet" href="css/style.css"> 
       <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'> 
       <link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png"> 
       <style> 
       .head{ 
       margin:auto; 
       margin-top: 40px; 
       margin-bottom: 40px; 
       width: 500px; 
       height: 50px; 
       text-align:center; 
      } 
       </style> 


       </head> 

       <body> 


        <div class="head"><h1> <span class="strong"></span></h1></div> 
       <div style="padding:0;" align="center" class="login-page"> 
        <img src="img/oms.png"><br><br> 
       <div class="form" > 


       <form class="login-form" name="frm" action="Logging.php" method="POST"> 
        <input type="text" placeholder="username" name="usrname"/> 
        <input type="password" placeholder="password" name="password"/> 
         <button type="submit" onclick="return logincheck()">login</button> 

        <p class="message"> Forgot Password <a href="forgotpass1.php">Click here</a></p> 
       </form> 
       </div> 
      </div> 
       <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

       <script type="text/javascript"> 

       function logincheck() 
        { 
        var x = document.frm.usrname.value; 
        var y = document.frm.password.value; 
        if(x =="" || x == null){ 
        alert("Enter the Username "); 
        return false; 
        } 
        else if(y=="" || y == null){ 
        alert("Enter the Password "); 
        return false; 
        }else{ 
        return true; 
        } 
        } 




       $('.message a').click(function(){ 
       $('form').animate({height: "toggle", opacity: "toggle"}, "slow"); 
      }); 
       </script> 




       </body> 
       <?php include 'footer1.php';?> 
      </html> 

     Logout.php 

     <?php 

     include 'header.php'; 
     include 'footer.php'; 




     session_destroy(); 

     echo "<script>alert('Successfully Logged Out');window.location.href='index.php'</script>"; 

     ?> 

    forgotpass1.php 

    <!DOCTYPE html> 
    <html > 
     <head> 
     <meta charset="UTF-8"> 
     <title></title> 
     <link rel="stylesheet" href="css/style.css"> 
     <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'> 
     <link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png"> 
     <style> 
     .head{ 
     margin:auto; 
     margin-top: 40px; 
     margin-bottom: 40px; 
     width: 500px; 
     height: 50px; 
     text-align:center; 
    } 
     </style> 


     </head> 

     <body> 


      <div class="head"><h1> <span class="strong"></span></h1></div> 
     <div style="padding:0;" align="center" class="login-page"> 
      <img src="img/oms.png"><br><br> 
     <div class="form" > 


     <form class="login-form" name="frm" action="validateemail1.php" method="POST"> 
      <input type="text" placeholder="Email" name="email"/> 
      <table width="100%"> 
      <tr><td align="left"> 
       <button type="submit" name="Back" value="Back" onclick="history.go(-1);" >Back</button></td><td>&nbsp </td><td align="left"> <button type="submit" name="submit" onclick="return logincheck()">Send Email</button></td></tr></table> 

     </form> 
     </div> 
    </div> 
     <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

     <script type="text/javascript"> 

     function logincheck() 
      { 

      var y = document.frm.email.value; 
      if(y=="" || y == null){ 
      alert("Enter the Email "); 
      return false; 
      }else{ 
      return true; 
      } 
      } 




     $('.message a').click(function(){ 
     $('form').animate({height: "toggle", opacity: "toggle"}, "slow"); 
    }); 
     </script> 




     </body> 
    </html> 
    <?php include 'footer1.php';?> 


    validateemail1.php 

    <?php 
    include 'dbConfig.php'; 


    if (isset($_POST['submit'])){ 
    $email=$_POST['email']; 


    $n=0; 

        $query=mysqli_query($con,"SELECT * FROM signup where email ='".$email."'"); 

        while($row=mysqli_fetch_array($query)) 
        { 
        $db_email=$row['email']; 
        if($db_email==$email) 
        {    
        $n++; 
        $to=$row['email']; 
        $subject = "Your Password "; 
        $txtn = '<table align="center" border="0" cellpadding="0" cellspacing="0" width="1000"> 
     <tr> 
      <td align="center" bgcolor="#2ce0e8" style="padding: 7px 0 10px 0;background:#f55322 "> 
       <img src="http://saiss.co.in/supreme_oms/img/oms.png" alt="http://saiss.co.in/supreme_oms/index" width="84" height="36" style="display: block;" /> 
      </td> 
     </tr> 
     <td bgcolor="#ffffff" style="padding: 20px 0 30px 0"><center>Hi ,'.$row["username"].'<br> 
     Your password is: '.$row["password"].'<br> <a href="http://saiss.co.in/supreme_oms/index.php">Click to Login</a></center> 
     </td> 
     <tr> 
      <td bgcolor="#f55322" style="padding: 25px 0px 18px 23px;color: #fff;font-size: 12px;"> 
       &copy; <?php echo date("Y"); ?> OMS All RIGHTS RESERVED. 
      </td> 
      <td align="right"> 
      </td> 
     </tr> 
    </table>'; 

     $headers = "MIME-Version: 1.0" . "\r\n"; 
      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
      $headers .= 'From: <OMS>' . "\r\n"; 


        mail($to,$subject,$txtn,$headers); 
        echo "<script>alert('We Sent Password To Your Email Address');window.location.href='index.php';</script>"; 
        } 
        } 
        if($n==0) 
        { 
        echo "<script>alert('Your email is Not Matching with our database');window.location.href='index.php';</script>"; 
        } 
        }    
    ?> 
Logging.php 
<?php 
session_start(); 
include 'dbConfig.php'; 
$logname = $_POST['usrname']; 
$logpass = $_POST['password']; 

if(isset($_POST['usrname'])) { 
    $name = $_POST['usrname']; 
} 

if(isset($_POST['password'])) { 
    $name = $_POST['password']; 
} 
if($logname != null && trim($logname) !="" && trim($logpass) !="" && $logpass !=null) 
{ 
    $getvalue =""; 
    $sql_query = "Select * from signup where username='".$logname."'and password ='".$logpass."'"; 
    $changepass=""; 
    $result_set = mysqli_query($con,$sql_query); 

    if(mysqli_num_rows($result_set)==0){ 
     echo "<script>alert('Invalid Username/Password');window.location.href='index.php'</script>";   
    }else{ 
     while($row=mysqli_fetch_row($result_set)) 
     { 
     $getvalue = $row[0]; 
     $changepass = $row[3]; 
     } 
     $_SESSION["usrnam"] = $getvalue; 
     if($changepass=="Y"){ 
      echo "<script>window.location.href='changepassword.php'</script>"; 
     }else 
     { 
     echo "<script>window.location.href='dashboard.php'</script>"; 
     } 
    } 

}else{ 
    echo "<script>alert('Invalid Username/Password');window.location.href='index.php'</script>";   
} 
?>