Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

PHP Register and Login scripts on same page not working

General discussions related to php

Moderators: macek, egami, gesf

PHP Register and Login scripts on same page not working

Postby iOSMaster » Sat Mar 29, 2014 4:30 pm

Hello

I am relatively new to php and web programming. I followed a tutorial to make user accounts with email verification (I branched off a little to suit my needs). I have to log in and register fields on the same page, but when I log in, it does no redirect me to the home.php page. Here is the code:

Code: Select all
<?php

            $msg = '';
           
            $hash = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable.


            //conect to our database
            mysql_connect("*****", "*****", "*****") or die("MySQL Error: " . mysql_error()); // Connect to                                                                                        database server(localhost) with username and password.
            mysql_select_db("*****") or die("MySQL Error: " . mysql_error()); // Select registrations database.
 
            //if((isset($_POST['name']) && !empty($_POST['name'])) AND (isset($_POST['email']) && !empty($_POST['email']))){
            if((!empty($_POST['name'])) AND (!empty($_POST['email'])) AND (!empty($_POST['password'])) AND (!empty($_POST['password1']))){
                // Form Submited
                $name = mysql_real_escape_string($_POST['name']); // Turn our post into a local variable
                $email = mysql_real_escape_string($_POST['email']); // Turn our post into a local variable
                $password = mysql_real_escape_string($_POST['password']);//turn our post into a local variable
                $password1 = mysql_real_escape_string($_POST['password1']); // turon out post into a local variable
               
                //run a check to see if the email is previously used
                $sql_email_check = mysql_query("SELECT email FROM users WHERE email='".$email."'") or die(mysql_error());
                $email_check = mysql_num_rows($sql_email_check);
               
                //verify the email is legit
                //if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
                if(!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)){
                   
                    // Return Error - Invalid Email
                    $msg = 'The email you have entered is invalid. Please try again.';
                   
                    //triggers an error
                    /*if(isEmailAlreadyInDatabase(mysql_real_escape_string($_POST['email']) == true)){
                       
                        $msg = 'The email you have entered is already in use. Please try another one.'
                           
                    }else{
                   
                        //do nothing
                   
                    }*/
                }else if($password != $password1){
                   
                    // let the user know that their passwords do not match
                    $msg = 'Passwords do not match!';
                   
                }else if ($email_check > 0){
                   
                    $msg = 'Email has already been used!';
                   
                }else{
                   
                    // Return Success - Valid Email
                    $msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has
                    been sent to your email.';
                   
                    mysql_query("INSERT INTO users (username, password, email, hash) VALUES(
                        '". mysql_real_escape_string($name) ."',
                        '". mysql_real_escape_string(md5($password)) ."',
                        '". mysql_real_escape_string($email) ."',
                        '". mysql_real_escape_string($hash) ."') ") or die(mysql_error());
                }
                   
            }

            //mail the new user
            $to      = $email; // Send email to our user
            $subject = 'Sign Up | Verification'; // Give the email a subject
            $message = '
 
                Thanks for signing up!
                Your account has been created, you can login with the following credentials after you have activated your account
                by pressing the url below.
 
                ------------------------
                Username: '.$name.'
                Password: '.$password.'
                ------------------------
 
                Please click this link to activate your account:
                http://strings.atwebpages.com/verify.php?email='.$email.'&hash='.$hash.'
 
            '; // Our message above including the link
                     
            $headers = 'From:info@strings.atwebpages.com' . "\r\n"; // Set from headers

            mail($to, $subject, $message, $headers); // Send our email

            //header('Location: strings.atwebpages.com');
            //exit();

           

            //start the code for loggin in
              if(isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['pass']) && !empty($_POST['pass'])){
                $username = mysql_escape_string($_POST['username']);
                $password = mysql_escape_string(md5($_POST['pass']));
                 
                $search = mysql_query("SELECT username, password, active FROM users WHERE username='".$username."' AND password='".$password."'AND active='1'") or die(mysql_error());
                   
                $match  = mysql_num_rows($search);
           
                if($match > 0){
               
                    // Set cookie / Start Session / Start Download etc...
                    //redirect to home page
                    //'<meta http-equiv="refresh" content="0;home.php">'
                    header("Location: home.php");
               
                }else{
               
                    $error = 'Login Failed! Please make sure that you enter the correct details and that you have activated your account.';
               
                }
           
            }
             
             
            ?>
           
            <!-- Stop PHP code -->
           
            <!-- Title and Description -->
           
            <h3> Sign Up </h3>
            <p> Please enter your name and email in order to sign up. </p>
       
           
            <?php

            if(isset($msg)){ 
               
                // Check if $msg is not empty
                echo '<div class="statusmsg">'.$msg.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".
               
            }
            ?>


All and any help is greatly appreciated!

Thank You!!
iOSMaster
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Mar 29, 2014 4:25 pm

Re: PHP Register and Login scripts on same page not working

Postby seandisanti » Sun Mar 30, 2014 5:44 pm

Is the login successful? Are you getting any errors, or just nothing happens after login? If it's the latter, output the values you're comparing to determine success, both of them.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: PHP Register and Login scripts on same page not working

Postby iOSMaster » Mon Mar 31, 2014 8:03 am

I am not getting any errors and nothing happens afterwards. I tried using echo to see the results, but the echo never showed up
iOSMaster
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Mar 29, 2014 4:25 pm

Re: PHP Register and Login scripts on same page not working

Postby seandisanti » Thu Apr 03, 2014 8:53 pm

on your login page, call phpinfo(); to make sure that it displays and php is running.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.