Search code examples
phpmysqlif-statementcrypt

if-statement doesnt work while crypt


i would like to have a page where i can change passwords. me should be the only one who have access to it. so my thoughts were to save my ip address and crypt it before storing it in the db. my first trying was to type in the ip by hand. then i thought, it would be much easier and safer as well, to get my ip address what i got in a second file called ip.php by:

$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
    $http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote_addr = $_SERVER['REMOTE_ADDR'];

    if (!empty($http_client_ip)){
        $ip_address = $http_client_ip;
    }
        else if (!empty($http_x_forwarded_for)){
            $ip_address = $http_x_forwarded_for;
    }else{
        $ip_address = $remote_addr;
    }

now this is what i've been adding to my script, that looks like that:

if(isset($_POST['submit'])){
    include_once "db_connect.php";
    include_once "ip.php";

    $IP = $ip_address;

    $masterkey = $_POST['masterkey'];
    $masterkey2 = $_POST['masterkey2'];


    if(empty($masterkey)||empty($masterkey2)){
        if(empty($masterkey)){
            $errors[]="there is no key1";
        }
        if(empty($masterkey2)){
            $errors[]="there is no key2";
        }

    }else{

        $masterkey = strip_tags($masterkey);
        $masterkey = stripslashes($masterkey);
        $masterkey = trim($masterkey);
        $masterkey = $db->real_escape_string($masterkey);

        $masterkey2 = strip_tags($masterkey2);
        $masterkey2 = stripslashes($masterkey2);
        $masterkey2 = trim($masterkey2);
        $masterkey2 = $db->real_escape_string($masterkey2);

        $IP = strip_tags($IP);
        $IP = stripslashes($IP);
        $IP = trim($IP);
        $IP = $db->real_escape_string($IP);

        $db_IP = crypt($ip_address, '$2a$12$password');
                ...start queries

now my problem is, that the else statement doesnt work. for a reason i dont know why this isn't working. i also was using errorreport and get the message:

Notice: Undefined variable: db_IP in ...  

i also tried var_dump but there was no error shown up. but when i will echo

$IP and $ip_address

that will display the ip the correct way. so i do not understand why this wont work. thanks.

UPDATE

okay, when error-reporting is set to e_all it will display

Notice: Undefined variable: db_IP in /var/www/web775/html/scripts/masterchange.php on line 179 

which relates to

<?php echo $db_IP;?> 

while the variable will be defined as

$db_IP = crypt($IP, '$2a$12$password');

and

$IP = $ip_address; 

and

$ip_address

comes from the ip.php

UPDATE2

okay, i solved the problem. the failure was, that there is an if-else condition. i just had a watch on the else statement and forgot about what's the condition in the if statement to get to the else-statement first. i tried to get an output without the conditions of the if-statement. so for sure, there couldn`t have been generated an output when the first phrase didn't take place. thats why var_dump was empty and didnt showed any error because there was no error :) thanks alot for trying to help me. i really appreciate that.


Solution

  • Notice: Undefined variable: db_IP in ...  
                                         ^^^
    

    These dots probably contain a file name and line in the original message. Your most immediate error is there: open it in your favourite editor end find the variable usage in that line. My educated guess is that you are using that variable from a function that doesn't receive it as argument.

    Another easy-to-spot error:

    $masterkey = $_POST[$IP];
    $masterkey = $_POST['masterkey'];
    

    You read $_POST[$IP] and discard it in the next line.

    Edit: So the error is in some other piece of code you didn't even mention before. That's why you need to read error messages: they are there to help, not to annoy. Now, what kind of further help do you need exactly? You cannot read $db_IP unless you write it first.