直入主題,先看php校驗登錄名和密碼是否正確的代碼:
<?php $servername = "服務器名"; $username = "賬戶名"; $password = "密碼"; $dbname = "數據庫名"; ?> <?php // Session需要先啟動。 session_start(); //判斷uname和pwd是否賦值 if(isset($_POST['uname']) && isset($_POST['pwd'])){ $name = $_POST['uname']; $pwd = $_POST['pwd']; //連接數據庫 $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //驗證內容是否與數據庫的記錄吻合。 $sql = "SELECT * FROM test_students_all WHERE (student_name='$name') AND (password='$pwd')"; //執行上面的sql語句並將結果集賦給result。 $result = $conn->query($sql); //判斷結果集的記錄數是否大於0 if ($result->num_rows > 0) { $_SESSION['user_account'] = $name; // 輸出每行數據 while($row = $result->fetch_assoc()) { echo '<p>' . $row['student_nbr'] . '<br/>' . $row['student_name'] . '(' . $row['sex'] . ')' . '<br/>' . $row['class'] . '<br/>' . $row['major'].'</p>'; // <p><img src="student_images/CLASS/STUDENT_NBR.jpg" /></p> echo '<p><img src="student_images/' . $row['class'] . '/' . $row['student_nbr'] . '.jpg" /></p>'; } } else { echo "沒有您要的信息"; } $conn->close(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄校驗</title> </head> <body> <p> <?php // isset(xx) 測試xx是否設置了 if(isset($_SESSION['user_account'])){ echo '你好,' . $_SESSION['user_account']; } else{ echo '游客'; } //$conn->close(); ?> </p> <form method="POST"> <input type="text" name="uname" placeholder="用戶名" /> <br /> <input type="password" name="pwd" placeholder="密碼" /> <br /> <input type="submit"> </form> </body> </html>
效果圖:
以上就是本文的全部內容,希望對大家學習php程序設計有所幫助。