gpt4 book ai didi

php - 无法登录和重定向 mysql php

转载 作者:行者123 更新时间:2023-11-29 03:22:28 24 4
gpt4 key购买 nike

我似乎无法使用我的代码登录和重定向!我已经尝试了一段时间,但我无法找到错误,因为我确信我以前使用过这段代码。

你能帮我告诉我哪里出错了吗?

索引.php:

<?php
//includes login script
include( 'login.php' );
?>

<!doctype html>
<html>
<head>
<title>Home Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/template.css" rel="stylesheet" type="text/css">
<script src="respond.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div id="login">
<h2>- Login -</h2>
<hr/>
<form action="" method="post">
<label>UserName :</label>
<input type="text" name="username" id="name" placeholder="username"/><br /><br />
<label>Password :</label>
<input type="password" name="password" id="password" placeholder="**********"/><br><br>
<br><br>
<input type="submit" value=" Login " name="submit"/><br />
<span><?php echo $error; ?></span>
</form>
</div>
</body>
</html>

登录.php

<?php
//starting session
session_start();

//variable to store error message
$error='';

if ( isset( $_POST[ 'submit' ] ) )
{
if ( empty( $_POST[ 'username' ] ) || empty( $_POST[ 'password' ] ) )
{
$error = "Information is invalid";
} else {
// Define $username and $password
$username = $_POST[ 'username' ];
$password = $_POST[ 'password' ];

// To protect MySQL injection for Security purpose
$username = stripslashes( $username );
$password = stripslashes( $password );

$username = mysql_escape_string( $username );
$password = mysql_escape_string( $password );

//encrypt the password
$password = md5 ( $password );

//Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect( "localhost", "user", "pass");

//Selecting Database
$db = mysql_select_db( "database", $connection );

//SQL query to fetch information of registerd users and finds user match.
$query = mysql_query( "SELECT * FROM users2 WHERE password='$password' AND username='$username'", $connection );

//perform query
$num_rows = mysql_num_rows($query);

if ($num_rows > 0) {
header("location: profile.php"); // Redirecting To Other Page
}
else{
$error = "nope";
}
//if ($rows == 1) {
// $_SESSION['username']=$username; // Initializing Session
//} else {
//}
//Closing Connection
mysql_close( $connection );
}
}
?>

最佳答案

只需从 index.php 中删除 include( 'login.php' ); 因为 login.php 已经加载了 index.php 发现没有post请求。

然后用

修改你的表单 Action
<form action="" method="post">

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

关于php - 无法登录和重定向 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41868292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com