重定向功能不起作用

时间:2014-07-16 13:02:19

标签: php mysql redirect mysqli

我正在尝试从登录页面获取密码,然后尝试从mysql收集存储的密码。然后我将它与提交的密码进行比较。现在我想要如果两个密码匹配然后重定向到页面。但问题是每次页面重定向。如果密码甚至与页面重定向不匹配。

<?php
if (redirect()==true){
    header('location:http://google.com');
}
if(redirect()!=false){
    echo "password doesnt match";
}
$hostname="localhost";
$dbname="justlive_main";
$dbusername="justlive_main";
$dbpass="Kalkata1";
#Veriable collected From Login Panel
$Submitted_Email_Address=$_POST['email'];
$Submitted_Password=$_POST['password'];
$encrypt_Submitted_Password=md5($Submitted_Password);

#Lets gather Data From MySql Database
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname);
if (mysqli_connect_errno($con)) {
    die('The connection to the database could not be established.');
}
$query="SELECT * FROM users WHERE Email_Address='$Submitted_Email_Address'";
$result=$con->query($query);
$row=$result->fetch_array();
$SQL_Password = $row['Password'];
function redirect(){
    if ($encrypt_Submitted_Password!=$SQL_Password) {
        return false;
    }
    elseif ($encrypt_Submitted_Password == $SQL_Password) {
        return true;
    }
}
#Lets CLose All The MySql Connection
$result->free();
$con->close();
?>

我已删除该功能并尝试以简单的方式执行此操作。但仍然无法正常工作。 编辑代码:

    <?php 

if ($encrypt_Submitted_Password!=$SQL_Password) {
       echo "Please check your Password";
}
elseif ($encrypt_Submitted_Password == $SQL_Password) {
        header('location:http://google.com');
}
$hostname="localhost";
$dbname="justlive_main";
$dbusername="justlive_main";
$dbpass="Kalkata1";
#Veriable collected From Login Panel
$Submitted_Email_Address=$_POST['email'];
echo "Submited Email Address:". $Submitted_Email_Address."<br>";
$Submitted_Password=$_POST['password'];
echo "Submitted Password : ".$Submitted_Password. "<br>";
$encrypt_Submitted_Password=md5($Submitted_Password);
echo "Encrypted Submitted_Password:".$encrypt_Submitted_Password."<br>";

#Lets gather Data From MySql Database
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname);
if (mysqli_connect_errno($con)) {
    die('The connection to the database could not be established.');
}
$query="SELECT * FROM users WHERE Email_Address='$Submitted_Email_Address'";
$result=$con->query($query);
$row=$result->fetch_array();
$SQL_Password = $row['Password'];
echo "Mysql Database Stored Password: ".$SQL_Password."<br>";


#Lets CLose All The MySql Connection
$result->free();
$con->close();

?>

4 个答案:

答案 0 :(得分:1)

在设置$ encrypt_Submitted_Password和SQL_Password之前,您正在调用redirect()函数。移动&#34; ifs&#34;在他们被初始化之后。你必须宣布它们是全球性的。或者更好的是,将变量作为参数传递

答案 1 :(得分:0)

您的重定向功能不正确:$encrypt_Submitted_Password$SQL_Password不在功能范围内,因此会返回通知错误,但您可以使用全局为此:

function redirect(){

    global $encrypt_Submitted_Password, $SQL_Password

    if ($encrypt_Submitted_Password!=$SQL_Password) {
        return false;
    }else{
        return true;
    }
}

或者您可以将两个变量作为参数传递给函数。

正如@Guilleva所说,你在实例化之前调用它:

你的代码应该是这样的:

<?php

function redirect(){
    if ($encrypt_Submitted_Password!=$SQL_Password) {
        return false;
    }else{
        return true;
    }
}


$hostname="localhost";
$dbname="justlive_main";
$dbusername="justlive_main";
$dbpass="Kalkata1";
#Veriable collected From Login Panel
$Submitted_Email_Address=$_POST['email'];
$Submitted_Password=$_POST['password'];
$encrypt_Submitted_Password=md5($Submitted_Password);

#Lets gather Data From MySql Database
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname);
if (mysqli_connect_errno($con)) {
    die('The connection to the database could not be established.');
}
$query="SELECT * FROM users WHERE Email_Address='$Submitted_Email_Address'";
$result=$con->query($query);
$row=$result->fetch_array();
$SQL_Password = $row['Password'];

if (redirect()==true){
    header('location:http://google.com');
}else{
    echo "password doesnt match";
}

#Lets CLose All The MySql Connection
$result->free();
$con->close();
?>

答案 2 :(得分:0)

我检查了你的代码并在加载了所有变量后带上了header命令。它现在在我的工作。我认为在准备好之前你调用了$ SQL_Password变量。请看一下,如果有效,请告诉我。

<?php 
$hostname="localhost";
$dbname="justlive_main";
$dbusername="justlive_main";
$dbpass="Kalkata1";
#Veriable collected From Login Panel
$Submitted_Email_Address=$_POST['email'];
#echo "Submited Email Address:". $Submitted_Email_Address."<br>";
$Submitted_Password=$_POST['password'];
#echo "Submitted Password : ".$Submitted_Password. "<br>";
$encrypt_Submitted_Password=md5($Submitted_Password);
#echo "Encrypted Submitted_Password:".$encrypt_Submitted_Password."<br>";

#Lets gather Data From MySql Database
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname);
if (mysqli_connect_errno($con)) {
    die('The connection to the database could not be established.');
}
$query="SELECT * FROM users WHERE Email_Address='$Submitted_Email_Address'";
$result=$con->query($query);
$row=$result->fetch_array();
$SQL_Password = $row['Password'];
#echo "Mysql Database Stored Password: ".$SQL_Password."<br>";
if ($encrypt_Submitted_Password!=$SQL_Password) {
       echo "Please check your Password";
}
elseif ($encrypt_Submitted_Password == $SQL_Password) {
        header('location:dashboard.php');
}   
#Lets CLose All The MySql Connection
$result->free();
$con->close();

?>

答案 3 :(得分:-1)

您的代码是

if (redirect()==true){
header('location:http://google.com');
}
if(redirect()!=false){
echo "password doesnt match";
}

redirect()==trueredirect()!=false的含义相同

所以你的代码应该是

if (redirect() == true){
header('location:http://google.com');
}
if (redirect() == false){
echo "password doesnt match";
}