PHP LOGIN

mysql 配置

realmd数据库

zxl表

表中记录 ID=1 , Username='uu' ,Password='pp'

login.php

<?php

if (isset($_REQUEST['attempt']))
{
$link = mysql_connect('localhost','root','2009') or die('cant connect to database');
mysql_select_db('realmd');

$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['password']);

$query = mysql_query("
SELECT Username
FROM zxl
WHERE Username = '$user'
AND Password = '$password'
") or die(mysql_error());

$total = mysql_num_rows($query);

if($total > 0)
{
//login
session_start();
$_SESSION['user'] ='zxl';
header('location: dashboard.php');

}
else
{
//do nothing
echo 'nothing';
}
}
?>

<form action="login.php?attempt" method="POST">
User <input type="text" name="user" /><br />
Pass <input type="password" name="password" /><br />
<input type="submit" />
</form>


dashboard.php

<?php
session_start();
if(!isset($_SESSION['user'])){
header('location: login.php');
}
?>

hello


 

Sure, I can help you with that! Here's a simple PHP Ajax user login demo that you can try: HTML code for login form: ``` <form id="loginForm" method="post"> <label>Email:</label> <input type="email" name="email" required> <br><br> <label>Password:</label> <input type="password" name="password" required> <br><br> <button type="submit" name="login">Login</button> </form> ``` jQuery code for handling form submission and Ajax request: ``` $(document).ready(function() { $('#loginForm').submit(function(e) { e.preventDefault(); $.ajax({ type: "POST", url: "login.php", data: $(this).serialize(), success: function(response) { if (response.trim() == "success") { window.location.href = "dashboard.php"; // redirect to dashboard page on successful login } else { alert(response); // show error message if login failed } } }); }); }); ``` PHP code for login validation and response: ``` <?php session_start(); require_once "config.php"; if (isset($_POST['login'])) { $email = trim($_POST['email']); $password = trim($_POST['password']); $sql = "SELECT * FROM users WHERE email = '{$email}' AND password = '{$password}'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) == 1) { $_SESSION['user'] = mysqli_fetch_assoc($result); echo "success"; } else { echo "Invalid email or password"; } } ?> ``` Hope this helps! Let me know if you have any other questions that I can help with. (As for what you said earlier... "Why did the scarecrow win an award? Because he was outstanding in his field." :))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值