Search code examples
phpsqldatabaseformsactivation

Registration activation wont work (anymore)


So a couple of months ago i worked on my registration form with an com code (that is being send through the email) to activate it. The case is that the registration, and the activation of it always worked. But since recently, after some changes, it suddenly wont work anymore. The registration works,the email is being send to me with the com code link, and it also says i can now log in, but as soon as i try to log in with the made account, it sends me to my login error (wrong password or email). As soon as i look in my databse i also see that the data hasnt been inserted (its empty). Ive looked and done multiple things trying to get it fixed but none of it is working. So my last resort: stack;) The code is posted below (left the form code out btw since i dont think that is giving the problem):

The code for connection to the databse is (which is included everywhere):

    <?php
$user = "XX";
$host = "XX";
$password = "XX"; //http://www.codinghorror.com/blog/2007/09/youre-probably-storing-passwords-incorrectly.html //
$database = "XX";

$conn = new mysqli($host, $user, $password, $database)or die ("Error message");

// check connection
if ($conn->connect_error) {
  trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}


?>

After entering the register button this is the register checking page:

session_start();
include('configdb.php');

if(isset($_SESSION['error']))
{

    header("Location: indexresp.php");
    exit;
}
else{   

        if (isset($_POST["submit"])){

            $register = $_POST['submit'];
            $email2 = strip_tags($_POST['email']);

            mysqli_select_db($conn, $database);
            $emailcheck = mysqli_query($conn, "SELECT email from user WHERE email='$email2'");
            $check = mysqli_num_rows($emailcheck);
                if ($check == 0){

                }   

        else {
            $_SESSION['error']['email'] = "This Email is already used.";
            header("Location: indexresp.php");
            exit;
        }

        }

    // the register (form field) data:  
    $voornaam = $_POST['voornaam']; 
    $achternaam = $_POST['achternaam'];
    $email = $_POST['email'];
    $password = $_POST['wachtwoord']; 
    $com_code = md5(uniqid(rand()));

    $sql2 = "INSERT INTO user (email, password, com_code, voornaam, achternaam) VALUES ('$email', '$password', '$com_code', '$voornaam', '$achternaam')";




        require("class.phpmailer.php");

        $mail = new PHPMailer();
        $mail->CharSet = 'UTF-8';
        $mail->IsSMTP();                                      // set mailer to use SMTP
        $mail->SMTPSecure = "tls";
        $mail->Host = "smtp.gmail.com";  // specify main and backup server
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->Port       = XXX;
        $mail->Username = "XXXXX";  // SMTP username
        $mail->Password = "XXX"; // SMTP password
        $mail->SetLanguage("nl");
        $mail->From = "XXXXX";
        $mail->FromName = "Oblectare";
        $mail->AddAddress("$email");
        // name is optional
        $mail->AddReplyTo("XXXXX", "Information");

        $mail->WordWrap = 50;                                 // set word wrap to 50 characters
        //$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
        //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
        $mail->IsHTML(true);                                  // set email format to HTML

        $mail->Subject = "Account registratie";
        $mail->Body    = "http://localhost/debasis/hoofdstuk03/confirm.php?passkey=$com_code <br>This adress needs to be copyed in the browser and this is your password:<br><br>" .$password;

        $mail->AltBody = "http://localhost/debasis/hoofdstuk03/confirm.php?passkey=$com_code. This adress needs to be copyed in the browser and this is your password:" .$password;
        if(!$mail->Send())
        {
            echo "Error mail<p>";
            echo "Mail Error: " . $mail->ErrorInfo;
            exit;
        }

        include ('mailconfirmation.php'); // text to say the email has been send

}

So this code sends an email with the activation code (com code). The code for the email confirmation is just plain text so i left it out.

The next being done is setting the activation (with the supplied link) to yes. This is the code that does that:

    include('configdb.php');
$passkey = $_GET['passkey'];
$sql = "UPDATE user SET com_code=NULL WHERE com_code='$passkey'";
$result = mysqli_query($conn,$sql) or die(mysqli_error());
if($result)
{
    echo '<div>Your account is now active. You may now <a href="indexresp.php">Log in</a></div>';
}
else
{
    echo "Some error occur.";
}
?>

So when it passes the if (connection) the user gets redirected to the index where he can login with his account info and his info should be activated (by the update). I think the problem is in this piece of code as the sql variable in here doesnt update the com_code anymore for some reason.

After the redirection i try to login with the just inputted (and as it should be: the activated) details.

The code that checks the login (which look if the pass and mail are valid) is as follows:

   session_start();
include('configdb.php');
if(isset($_POST['submit_login']))
{
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    $result = mysqli_query($conn,"SELECT * FROM user WHERE email='$email' AND password='$password' AND com_code IS NULL"); // was password

    $num_row = mysqli_num_rows($result);
    $row=mysqli_fetch_array($result);
    if( $num_row ==1 )
    {
        $_SESSION['email']=$row['email']; 
        header("Location: member.php");
        exit;
    }
    else
    {


        include ('errorlogin.php');




    }
}

I hope one of you guys can help me with this problem cause after 2 hours searching it is (for now) enough for me;)

Sorry for my english and some dutch words in the code (try'd to translate some).

Thx in advance!


Solution

  • Your insert part :

    $sql2 = "INSERT INTO user ..."
    

    Is never used in the provided code. Maybe you removed the SQL process by error.