2013-02-27 430 views
0

我对php比较陌生,我试图编写一个非常简单的登录脚本。我已经获得了基本的功能,但我无法登录到系统。我的登录脚本在下面,我的注册脚本也在下面。php登录身份验证失败

checklogin.php

include_once 'inc/db.inc.php'; 

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string(md5($_POST['password'])); 


try { 
$sql="SELECT id, username, password FROM users WHERE username='$username' and password='$password'"; 
$result = $pdo->query($sql); 
$count=mysql_num_rows($result); 
// If result matched $username and $password, table row must be 1 row 
if($count == 1){ 

    // Register $username, $password and redirect to file "index.php" 
    session_register("username"); 
    session_register("password"); 
    header("Location: index.php"); 
} 
else { 
header("Location: login.php?invalid=1"); 
} 
} 

catch (PDOException $e) { 
    echo $e; 
} 

ob_end_flush(); 

?> 

checkreg.php

include_once 'inc/db.inc.php'; 

//This makes sure they did not leave any fields blank 
if (!$_POST['username'] | !$_POST['password'] | !$_POST['passwordconf']) { 
    die('You did not complete all of the required fields'); 
} 

if ($_POST['password'] != $_POST['passwordconf']) { 
    die('Your passwords did not match. '); 
} 

$_POST['password'] = md5($_POST['password']); 
if (!get_magic_quotes_gpc()) { 
    $_POST['password'] = addslashes($_POST['password']); 
    $_POST['username'] = addslashes($_POST['username']); 
     } 
$username = $_POST['username']; 
$password = $_POST['password']; 

try { 
// now we insert it into the database 
$sql = "INSERT INTO users(username,password) VALUES ('$username','$password')"; 
$result = $pdo->exec($sql); 
header("Location: index.php"); 

} catch (PDOException $e){ 
echo $e; 
} 
?> 

我知道该登记写入数据库,但每次我尝试我收到我的证书无效标志有效的登录。任何你能做的事情都会很棒。谢谢。

+0

似乎不像你阻止人们注册相同的用户名/密码组合。你在桌上碰巧有不止一次相同的组合?这会阻止$ count从1,并导致你所看到的。 – Gyhth 2013-02-27 19:10:17

+0

您是否尝试在登录后尝试回显用户名和密码以查看它们是否与数据库匹配? – James 2013-02-27 19:11:42

回答

0

您的问题可能与session_register()有关,具体取决于您使用的PHP版本。尝试把

session_start(); 

checklogin.php的顶部,然后利用

... 
    $_SESSION['username'] = $username; 
    $_SESSION['password'] = $password; 
    ... 

代替了session_register()

+0

'code'session_start(); $ username = mysql_real_escape_string($ _ POST ['username']); $ password = mysql_real_escape_string(md5($ _ POST ['password'])); try { \t $ sql =“SELECT id,username,password FROM pinters WHERE username ='$ username'and password ='$ password'”; \t $ result = $ pdo-> query($ sql); \t $ count = mysql_num_rows($ result); ($ count = 0){ \t \t $ _SESSION [“username”] = $ username; \t \t $ _SESSION [“password”] =“validated”; \t \t header(“Location:index.php”); \t} \t else { \t echo $ username; \t echo $ password; \t // header(“Location:login.php?invalid = 1”); \t} } – user2116751 2013-02-27 19:38:38

+0

仍然无法验证 – user2116751 2013-02-27 19:39:16

+0

只是为了试试'echo $ _SESSION [“username”]“。如果没有显示任何内容,POST数据只是没有得到您的checklogin.php,这可能是您的登录表单中的错字 – 2013-02-27 19:47:19

0

首先,你应该清楚一些东西出来:

1.

if (!$_POST['username'] | !$_POST['password'] | !$_POST['passwordconf']) { 
die('You did not complete all of the required fields');} 

应该& &,而不是|。

  1. 在你的代码中无处检查用户名是否存在,所以我猜你有多个用户使用相同的用户名。在插入你的checkreg.php之前,你应该先检查用户名是否存在。

  2. 在checklogin.php变化,如果($计数== 1)

    到如果($计数> 0)

如果这没有帮助,你可以给我更多的像数据库中的数据信息(数据这是在你的数据库现在)

0
<?php 
// Use of session_register() is deprecated 
$barney = "A big purple dinosaur."; 
session_register("barney"); 

// Use of $_SESSION is preferred, as of PHP 4.1.0 
$_SESSION["zim"] = "An invader from another planet."; 

// The old way was to use $HTTP_SESSION_VARS 
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants."; 
?> 

警告:

此功能已被弃用的PHP 5.3.0和去除Ø f PHP 5.4.0。

0

几件事情尝试:

1)了session_register函数调用已取消。 http://php.net/manual/en/function.session-register.php。如果可能的话,你真的应该使用$ _SESSION。

事情是这样的:

$_SESSION["username"] = $username; 
$_SESSION["password"] = "validated"; // md5 is variable so you can't easily check for it on next page render 

2)当值的测试,你不想$ _ POST,你要到下一个页面呈现上使用$ _SESSION一次。事情是这样的:

if ("validated" == $_SESSION["password"]) { 
    // You're logged in... 
} 

3)注意,对于要填充$ _SESSION数组必须调用session_start();使用前一次且仅有一次。 (http://www.php.net/manual/en/function.session-start.php更多。)

4)我知道这是示例代码,但SQL注入攻击可能。需要逃避这些变量。请输入用户名和密码,然后点击“确定”。

+0

做出了你们建议的更改,但我仍然遇到问题。而不是做重定向到无效登录,我现在回显用户名和密码。但没有出现。 – user2116751 2013-02-27 19:29:23

+0

感觉应该有第三块代码丢失。什么是没有检查,看看你是否已经通过查看$ _SESSION变量登录,然后启动查找checklogin.php中的密码。 – 2013-02-28 17:42:09