php php中登录和数据库连接的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21409722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
code for login and database connect in php
提问by user3242335
It doesn't work and I've spent hours and half my hairs trying to work out why. I already had the form imbedded in my html index file with this code:
它不起作用,我花了几个小时半的时间试图找出原因。我已经使用以下代码将表单嵌入到我的 html 索引文件中:
I want to create login and logout session
我想创建登录和注销会话
My table in mysql database is looking like this
我在 mysql 数据库中的表看起来像这样
CREATE TABLE member ( id int(10) NOT NULL auto_increment, userName varchar(50) NOT NULL, passWord varchar(50) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM utf8_general_ci AUTO_INCREMENT=3 ;
<?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
$userName=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$passWord=md5($password); // Encrypted Password
//*********retrieving data from Database**********
$query = "SELECT * FROM member WHERE userName='$userName' and passWord='$passWord'";
//$login = mysqli_query("SELECT userName,password FROM 'member' WHERE userName= $_post['username'] AND passWord= $_post['password'])");
// Check username and password match
$res = mysql_query($query);
$rows = mysql_num_rows($res);
if ($rows==1) {
// Set username session variable
$_SESSION['userName'] = $_POST['username'];
// Jump to secured page
header("Location: securedpage.php");
}
else {
// Jump to login page
echo "user name and password not found";
}
exit;
?>
in this code while login it goes directly to user name and password not found even username and password is correct
在此代码中,登录时直接转到用户名和密码,即使用户名和密码正确也找不到
回答by vaske
Presume that you are using this only for learning purpose not for real application
假设您仅将其用于学习目的而不是用于实际应用
You should check your DB setting and take care that all things are in place, then you need an form from where you will do post to this php file, here is small update:
您应该检查您的数据库设置并注意所有内容都已就位,然后您需要一个表单,您将在其中发布到此 php 文件,这里有一个小更新:
<?php
session_start();
$hostname = 'localhost';
$dbname = 'yourdatabase';
$username = 'root';
$password = 'yourpassword';
mysql_connect($hostname, $username, $password) or DIE('Connection to host isailed, perhaps the service is down!');
mysql_select_db($dbname) or DIE('Database name is not available!');
$userName=mysql_real_escape_string($_POST['username']);
$passWord=md5(mysql_real_escape_string($_POST['password']));
$query = "SELECT id FROM member WHERE userName='$userName' and passWord='$passWord'";
$res = mysql_query($query);
$rows = mysql_num_rows($res);
if ($rows==1)
{
$_SESSION['userName'] = $_POST['username'];
header("Location: securedpage.php");
}
else
{
echo "user name and password not found";
// TODO - replace message with redirection to login page
// header("Location: securedpage.php");
}
?>
?>
This should works, but keep in mind this code has to be re written on different way otherwise this one have a big security issues etc..
这应该有效,但请记住,必须以不同的方式重新编写此代码,否则此代码会存在很大的安全问题等。
small correction on db setup
数据库设置的小修正
CREATE TABLE member ( id int(10) NOT NULL auto_increment, userName varchar(50) NOT NULL, passWord varchar(50) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM;
回答by thenish
Here is the code.... modify and try
这是代码....修改并尝试
<?php
session_start();
$connect = mysql_connect('localhost', 'root', '') or die('Database could not connect');
$select = mysql_select_db('test', $connect) or die('Database could not select');
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
chckusername($username, $password);
}
function chckusername($username, $password){
$check = "SELECT * FROM login WHERE username='$username'";
$check_q = mysql_query($check) or die("<div class='loginmsg'>Error on checking Username<div>");
if (mysql_num_rows($check_q) == 1) {
chcklogin($username, $password);
}
else{
echo "<div id='loginmsg'>Wrong Username</div>";
}
}
function chcklogin($username, $password){
$login = "SELECT * FROM login WHERE username='$username' and password='$password'";
$login_q = mysql_query($login) or die('Error on checking Username and Password');
if (mysql_num_rows($login_q) == 1){
echo "<div id='loginmsg'> Logged in as $username </div>";
$_SESSION['username'] = $username;
header('Location: member.php');
}
else {
echo "<div id='loginmsg'>Wrong Password </div>";
//header('Location: login-problem.php');
}
}
?>

