Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4

    need help with a registration script ??


    i have installed apache , php , mysql .. properly and have managed to make the setup work together ...

    httpd-2.4.18-win64-VC14
    php-7.0.2-Win32-VC14-x64
    mysql-installer-community-5.6.28.0.msi


    i found this tutorial online and i was trying to make it work ...

    Login Registration with Email Verification, Forgot Password using PHP | Coding Cage

    login and registration script with email verification along with forgot password recovery feature using PHP and MySQL











    index.php

    PHP Code:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Login | Coding Cage</title>
        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
      </head>
      <body id="login">
        <div class="container">
            <?php 
            
    if(isset($_GET['inactive']))
            {
                
    ?>
                <div class='alert alert-error'>
                    <button class='close' data-dismiss='alert'>&times;</button>
                    <strong>Sorry!</strong> This Account is not Activated Go to your Inbox and Activate it. 
                </div>
                <?php
            
    }
            
    ?>
            <form class="form-signin" method="post">
            <?php
            
    if(isset($_GET['error']))
            {
                
    ?>
                <div class='alert alert-success'>
                    <button class='close' data-dismiss='alert'>&times;</button>
                    <strong>Wrong Details!</strong> 
                </div>
                <?php
            
    }
            
    ?>
            <h2 class="form-signin-heading">Sign In.</h2><hr />
            <input type="email" class="input-block-level" placeholder="Email address" name="txtemail" required />
            <input type="password" class="input-block-level" placeholder="Password" name="txtupass" required />
             <hr />
            <button class="btn btn-large btn-primary" type="submit" name="btn-login">Sign in</button>
            <a href="signup.php" style="float:right;" class="btn btn-large">Sign Up</a><hr />
            <a href="fpass.php">Lost your Password ? </a>
          </form>

        </div> <!-- /container -->
        <script src="bootstrap/js/jquery-1.9.1.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
      </body>
    </html>

    <?php
    session_start
    ();
    require_once 
    'class.user.php';
    $user_login = new USER();

    if(
    $user_login->is_logged_in()!="")
    {
        
    $user_login->redirect('home.php');
    }

    if(isset(
    $_POST['btn-login']))
    {
        
    $email trim($_POST['txtemail']);
        
    $upass trim($_POST['txtupass']);
        
        if(
    $user_login->login($email,$upass))
        {
            
    $user_login->redirect('home.php');
        }
    }
    ?>
    signup.php

    PHP Code:
    <!DOCTYPE html>
    <html>
      <head>
        <title>Signup | Coding Cage</title>
        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
      </head>
      <body id="login">
        <div class="container">
                    <?php if(isset($msg)) echo $msg;  ?>
          <form class="form-signin" method="post">
            <h2 class="form-signin-heading">Sign Up</h2><hr />
            <input type="text" class="input-block-level" placeholder="Username" name="txtuname" required />
            <input type="email" class="input-block-level" placeholder="Email address" name="txtemail" required />
            <input type="password" class="input-block-level" placeholder="Password" name="txtpass" required />
             <hr />
            <button class="btn btn-large btn-primary" type="submit" name="btn-signup">Sign Up</button>
            <a href="index.php" style="float:right;" class="btn btn-large">Sign In</a>
          </form>

        </div> <!-- /container -->
        <script src="vendors/jquery-1.9.1.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
      </body>
    </html>
    <?php
    session_start
    ();
    require_once 
    'class.user.php';

    $reg_user = new USER();

    if(
    $reg_user->is_logged_in()!="")
    {
        
    $reg_user->redirect('home.php');
    }


    if(isset(
    $_POST['btn-signup']))
    {
        
    $uname trim($_POST['txtuname']);
        
    $email trim($_POST['txtemail']);
        
    $upass trim($_POST['txtpass']);
        
    $code md5(uniqid(rand()));
        
        
    $stmt $reg_user->runQuery("SELECT * FROM tbl_users WHERE userEmail=:email_id");
        
    $stmt->execute(array(":email_id"=>$email));
        
    $row $stmt->fetch(PDO::FETCH_ASSOC);
        
        if(
    $stmt->rowCount() > 0)
        {
            
    $msg "
                  <div class='alert alert-error'>
                    <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>Sorry !</strong>  email allready exists , Please Try another one
                  </div>
                  "
    ;
        }
        else
        {
            if(
    $reg_user->register($uname,$email,$upass,$code))
            {            
                
    $id $reg_user->lasdID();        
                
    $key base64_encode($id);
                
    $id $key;
                
                
    $message "                    
                            Hello 
    $uname,
                            <br /><br />
                            Welcome to Coding Cage!<br/>
                            To complete your registration  please , just click following link<br/>
                            <br /><br />
                            <a href='http://localhost/x/verify.php?id=
    $id&code=$code'>Click HERE to Activate :)</a>
                            <br /><br />
                            Thanks,"
    ;
                            
                
    $subject "Confirm Registration";
                            
                
    $reg_user->send_mail($email,$message,$subject);    
                
    $msg "
                        <div class='alert alert-success'>
                            <button class='close' data-dismiss='alert'>&times;</button>
                            <strong>Success!</strong>  We've sent an email to 
    $email.
                        Please click on the confirmation link in the email to create your account. 
                          </div>
                        "
    ;
            }
            else
            {
                echo 
    "sorry , Query could no execute...";
            }        
        }
    }
    ?>

    fpass.php

    PHP Code:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Forgot Password</title>
        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
      </head>
      <body id="login">
        <div class="container">

          <form class="form-signin" method="post">
            <h2 class="form-signin-heading">Forgot Password</h2><hr />
            
                <?php
                
    if(isset($msg))
                {
                    echo 
    $msg;
                }
                else
                {
                    
    ?>
                      <div class='alert alert-info'>
                    Please enter your email address. You will receive a link to create a new password via email.!
                    </div>  
                    <?php
                
    }
                
    ?>
            
            <input type="email" class="input-block-level" placeholder="Email address" name="txtemail" required />
             <hr />
            <button class="btn btn-danger btn-primary" type="submit" name="btn-submit">Generate new Password</button>
          </form>

        </div> <!-- /container -->
        <script src="bootstrap/js/jquery-1.9.1.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
      </body>
    </html>
    <?php
    session_start
    ();
    require_once 
    'class.user.php';
    $user = new USER();

    if(
    $user->is_logged_in()!="")
    {
        
    $user->redirect('home.php');
    }

    if(isset(
    $_POST['btn-submit']))
    {
        
    $email $_POST['txtemail'];
        
        
    $stmt $user->runQuery("SELECT userID FROM tbl_users WHERE userEmail=:email LIMIT 1");
        
    $stmt->execute(array(":email"=>$email));
        
    $row $stmt->fetch(PDO::FETCH_ASSOC);    
        if(
    $stmt->rowCount() == 1)
        {
            
    $id base64_encode($row['userID']);
            
    $code md5(uniqid(rand()));
            
            
    $stmt $user->runQuery("UPDATE tbl_users SET tokenCode=:token WHERE userEmail=:email");
            
    $stmt->execute(array(":token"=>$code,"email"=>$email));
            
            
    $message"
                       Hello , 
    $email
                       <br /><br />
                       We got requested to reset your password, if you do this then just click the following link to reset your password, if not just ignore                   this email,
                       <br /><br />
                       Click Following Link To Reset Your Password 
                       <br /><br />
                       <a href='http://localhost/x/resetpass.php?id=
    $id&code=$code'>click here to reset your password</a>
                       <br /><br />
                       thank you :)
                       "
    ;
            
    $subject "Password Reset";
            
            
    $user->send_mail($email,$message,$subject);
            
            
    $msg "<div class='alert alert-success'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        We've sent an email to 
    $email.
                        Please click on the password reset link in the email to generate new password. 
                      </div>"
    ;
        }
        else
        {
            
    $msg "<div class='alert alert-danger'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>Sorry!</strong>  this email not found. 
                    </div>"
    ;
        }
    }
    ?>
    resetpass.php

    PHP Code:
    <!DOCTYPE html>
    <html>
      <head>
        <title>Password Reset</title>
        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
      </head>
      <body id="login">
        <div class="container">
            <div class='alert alert-success'>
                <strong>Hello !</strong>  <?php echo $rows['userName'?> you are here to reset your forgetton password.
            </div>
            <form class="form-signin" method="post">
            <h3 class="form-signin-heading">Password Reset.</h3><hr />
            <?php
            
    if(isset($msg))
            {
                echo 
    $msg;
            }
            
    ?>
            <input type="password" class="input-block-level" placeholder="New Password" name="pass" required />
            <input type="password" class="input-block-level" placeholder="Confirm New Password" name="confirm-pass" required />
             <hr />
            <button class="btn btn-large btn-primary" type="submit" name="btn-reset-pass">Reset Your Password</button>
            
          </form>

        </div> <!-- /container -->
        <script src="bootstrap/js/jquery-1.9.1.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
      </body>
    </html>

    <?php
    require_once 'class.user.php';
    $user = new USER();

    if(empty(
    $_GET['id']) && empty($_GET['code']))
    {
        
    $user->redirect('index.php');
    }

    if(isset(
    $_GET['id']) && isset($_GET['code']))
    {
        
    $id base64_decode($_GET['id']);
        
    $code $_GET['code'];
        
        
    $stmt $user->runQuery("SELECT * FROM tbl_users WHERE userID=:uid AND tokenCode=:token");
        
    $stmt->execute(array(":uid"=>$id,":token"=>$code));
        
    $rows $stmt->fetch(PDO::FETCH_ASSOC);
        
        if(
    $stmt->rowCount() == 1)
        {
            if(isset(
    $_POST['btn-reset-pass']))
            {
                
    $pass $_POST['pass'];
                
    $cpass $_POST['confirm-pass'];
                
                if(
    $cpass!==$pass)
                {
                    
    $msg "<div class='alert alert-block'>
                            <button class='close' data-dismiss='alert'>&times;</button>
                            <strong>Sorry!</strong>  Password Doesn't match. 
                            </div>"
    ;
                }
                else
                {
                    
    $password md5($cpass);
                    
    $stmt $user->runQuery("UPDATE tbl_users SET userPass=:upass WHERE userID=:uid");
                    
    $stmt->execute(array(":upass"=>$password,":uid"=>$rows['userID']));
                    
                    
    $msg "<div class='alert alert-success'>
                            <button class='close' data-dismiss='alert'>&times;</button>
                            Password Changed.
                            </div>"
    ;
                    
    header("refresh:5;index.php");
                }
            }    
        }
        else
        {
            
    $msg "<div class='alert alert-success'>
                    <button class='close' data-dismiss='alert'>&times;</button>
                    No Account Found, Try again
                    </div>"
    ;
                    
        }
        
        
    }

    ?>
    class.user.php

    PHP Code:
    <?php

    require_once 'dbconfig.php';

    class 
    USER
    {    

        private 
    $conn;
        
        public function 
    __construct()
        {
            
    $database = new Database();
            
    $db $database->dbConnection();
            
    $this->conn $db;
        }
        
        public function 
    runQuery($sql)
        {
            
    $stmt $this->conn->prepare($sql);
            return 
    $stmt;
        }
        
        public function 
    lasdID()
        {
            
    $stmt $this->conn->lastInsertId();
            return 
    $stmt;
        }
        
        public function 
    register($uname,$email,$upass,$code)
        {
            try
            {                            
                
    $password md5($upass);
                
    $stmt $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass,tokenCode) 
                                                             VALUES(:user_name, :user_mail, :user_pass, :active_code)"
    );
                
    $stmt->bindparam(":user_name",$uname);
                
    $stmt->bindparam(":user_mail",$email);
                
    $stmt->bindparam(":user_pass",$password);
                
    $stmt->bindparam(":active_code",$code);
                
    $stmt->execute();    
                return 
    $stmt;
            }
            catch(
    PDOException $ex)
            {
                echo 
    $ex->getMessage();
            }
        }
        
        public function 
    login($email,$upass)
        {
            try
            {
                
    $stmt $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
                
    $stmt->execute(array(":email_id"=>$email));
                
    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
                
                if(
    $stmt->rowCount() == 1)
                {
                    if(
    $userRow['userStatus']=="Y")
                    {
                        if(
    $userRow['userPass']==md5($upass))
                        {
                            
    $_SESSION['userSession'] = $userRow['userID'];
                            return 
    true;
                        }
                        else
                        {
                            
    header("Location: index.php?error");
                            exit;
                        }
                    }
                    else
                    {
                        
    header("Location: index.php?inactive");
                        exit;
                    }    
                }
                else
                {
                    
    header("Location: index.php?error");
                    exit;
                }        
            }
            catch(
    PDOException $ex)
            {
                echo 
    $ex->getMessage();
            }
        }
        
        
        public function 
    is_logged_in()
        {
            if(isset(
    $_SESSION['userSession']))
            {
                return 
    true;
            }
        }
        
        public function 
    redirect($url)
        {
            
    header("Location: $url");
        }
        
        public function 
    logout()
        {
            
    session_destroy();
            
    $_SESSION['userSession'] = false;
        }
        
        function 
    send_mail($email,$message,$subject)
        {                        
            require_once(
    'mailer/class.phpmailer.php');
            
    $mail = new PHPMailer();
            
    $mail->IsSMTP(); 
            
    $mail->SMTPDebug  0;                     
            
    $mail->SMTPAuth   true;                  
            
    $mail->SMTPSecure "ssl";                 
            
    $mail->Host       "smtp.gmail.com";      
            
    $mail->Port       465;             
            
    $mail->AddAddress($email);
            
    $mail->Username="your_gmail_id_here@gmail.com";  
            
    $mail->Password="your_gmail_password_here";            
            
    $mail->SetFrom('your_gmail_id_here@gmail.com','Coding Cage');
            
    $mail->AddReplyTo("your_gmail_id_here@gmail.com","Coding Cage");
            
    $mail->Subject    $subject;
            
    $mail->MsgHTML($message);
            
    $mail->Send();
        }    
    }
    dbconfig.php

    PHP Code:
    <?php
    class Database
    {
         
        private 
    $host "localhost";
        private 
    $db_name "rootdb";
        private 
    $username "root";
        private 
    $password "root";
        public 
    $conn;
         
        public function 
    dbConnection()
        {
         
            
    $this->conn null;    
            try
            {
                
    $this->conn = new PDO("mysql:host=" $this->host ";dbname=" $this->db_name$this->username$this->password);
                
    $this->conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);    
            }
            catch(
    PDOException $exception)
            {
                echo 
    "Connection error: " $exception->getMessage();
            }
             
            return 
    $this->conn;
        }
    }
    ?>
    verify.php

    PHP Code:
    <!DOCTYPE html>
    <html>
      <head>
        <title>Confirm Registration</title>
        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="assets/styles.css" rel="stylesheet" media="screen">
         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
      </head>
      <body id="login">
        <div class="container">
            <?php if(isset($msg)) { echo $msg; } ?>
        </div> <!-- /container -->
        <script src="vendors/jquery-1.9.1.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
      </body>
    </html>

    <?php
    require_once 'class.user.php';
    $user = new USER();

    if(empty(
    $_GET['id']) && empty($_GET['code']))
    {
        
    $user->redirect('index.php');
    }

    if(isset(
    $_GET['id']) && isset($_GET['code']))
    {
        
    $id base64_decode($_GET['id']);
        
    $code $_GET['code'];
        
        
    $statusY "Y";
        
    $statusN "N";
        
        
    $stmt $user->runQuery("SELECT userID,userStatus FROM tbl_users WHERE userID=:uID AND tokenCode=:code LIMIT 1");
        
    $stmt->execute(array(":uID"=>$id,":code"=>$code));
        
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
        if(
    $stmt->rowCount() > 0)
        {
            if(
    $row['userStatus']==$statusN)
            {
                
    $stmt $user->runQuery("UPDATE tbl_users SET userStatus=:status WHERE userID=:uID");
                
    $stmt->bindparam(":status",$statusY);
                
    $stmt->bindparam(":uID",$id);
                
    $stmt->execute();    
                
                
    $msg "
                       <div class='alert alert-success'>
                       <button class='close' data-dismiss='alert'>&times;</button>
                          <strong>WoW !</strong>  Your Account is Now Activated : <a href='index.php'>Login here</a>
                       </div>
                       "
    ;    
            }
            else
            {
                
    $msg "
                       <div class='alert alert-error'>
                       <button class='close' data-dismiss='alert'>&times;</button>
                          <strong>sorry !</strong>  Your Account is allready Activated : <a href='index.php'>Login here</a>
                       </div>
                       "
    ;
            }
        }
        else
        {
            
    $msg "
                   <div class='alert alert-error'>
                   <button class='close' data-dismiss='alert'>&times;</button>
                   <strong>sorry !</strong>  No Account Found : <a href='signup.php'>Signup here</a>
                   </div>
                   "
    ;
        }    
    }

    ?>
    but i keep getting this errors ...

    Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\Apache24\htdocs\class.user.php:19 Stack trace: #0 C:\Apache24\htdocs\signup.php(53): USER->runQuery('SELECT * FROM t...') #1 {main} thrown in C:\Apache24\htdocs\class.user.php on line 19

    please help ...
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,113
    Rep Power
    2010
    Your code doesn't validate if dbConnection() returns a null on failure.
    Have you verified that the database connection can log-in successfully?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    MrFujin ,

    you mean this part ... ??

    dbconfig.php

    PHP Code:
    <?php
    class Database
    {
         
        private 
    $host "localhost";
        private 
    $db_name "rootdb";
        private 
    $username "root";
        private 
    $password "root";
        public 
    $conn;
         
        public function 
    dbConnection()
        {
         
            
    $this->conn null;    
            try
            {
                
    $this->conn = new PDO("mysql:host=" $this->host ";dbname=" $this->db_name$this->username$this->password);
                
    $this->conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);    
            }
            catch(
    PDOException $exception)
            {
                echo 
    "Connection error: " $exception->getMessage();
            }
             
            return 
    $this->conn;
        }
    }
    ?>
    i am not just getting any error messages , just a plain page when i click on that link ...
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,113
    Rep Power
    2010
    I mean what value does this line return:
    Code:
     return $this->conn;
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    i only get this ...

    i don't know what else this is supposed to do ...






  10. #6
  11. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,113
    Rep Power
    2010
    When you call prepare in runQuery, how do you know if the database connection has connected succesfully?
    For this test, try to replace:
    Code:
    echo "Connection error: " . $exception->getMessage();
    with:
    Code:
    die("Connection error: " . $exception->getMessage());
    and then post the result you receive now.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    i edited the dbconfig.php and replaced this line ...

    echo "Connection error: " . $exception->getMessage();
    with this ..

    die("Connection error: " . $exception->getMessage());
    still i am only getting this ...





    can i replace that dbconfig.php ...

    with this ??

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "root";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>
    Last edited by digimon; July 18th, 2016 at 08:14 AM.
  14. #8
  15. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,113
    Rep Power
    2010
    If you just tried it you would find the answer faster. you can use it for testing the database connection.

    Maybe you should add "error_reporting(-1);" at top of the code too:
    Code:
    <?php
    error_reporting(-1);
    $servername = "localhost";
    // rest of code ...

    Comments on this post

    • digimon agrees
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    this config.php is working properly ...

    PHP Code:
    <?php
    $servername 
    "localhost";
    $username "root";
    $password "root";

    // Create connection
    $conn = new mysqli($servername$username$password);

    // Check connection
    if ($conn->connect_error) {
    die(
    "Connection failed: " $conn->connect_error);
    }
    echo 
    "Connected successfully";
    ?>
    i have a total of 9 files from the tutorial in my htdocs folder ...

    index.php
    home.php
    signup.php
    fpass.php
    resetpass.php
    classuser.php
    dbconfig.php
    verify.php
    logout.php

    ...

    i thought after creating the database and table and giving it the proper database connection details , it would work ....

    i have few more doubts ...

    what is the differences between these two files ..??

    In this, and in the following chapters we demonstrate three ways of working with PHP and MySQL:

    MySQLi (object-oriented)
    MySQLi (procedural)
    PDO
    Notice that in the PDO example , we have also specified a database (myDB). PDO require a valid database to connect to. If no database is specified, an exception is thrown


    config.php

    PHP Code:
    <?php
    $servername 
    "localhost";
    $username "root";
    $password "root";

    // Create connection
    $conn = new mysqli($servername$username$password);

    // Check connection
    if ($conn->connect_error) {
    die(
    "Connection failed: " $conn->connect_error);
    }
    echo 
    "Connected successfully";
    ?>

    dbconfig.php

    PHP Code:
    <?php
    class Database
    {
         
        private 
    $host "localhost";
        private 
    $db_name "dbtest";
        private 
    $username "root";
        private 
    $password "";
        public 
    $conn;
         
        public function 
    dbConnection()
        {
         
            
    $this->conn null;    
            try
            {
                
    $this->conn = new PDO("mysql:host=" $this->host ";dbname=" $this->db_name$this->username$this->password);
                
    $this->conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);    
            }
            catch(
    PDOException $exception)
            {
                echo 
    "Connection error: " $exception->getMessage();
            }
             
            return 
    $this->conn;
        }
    }
    ?>

    i think this is the file that gives me most of the errors ...

    PHP Code:
    <?php

    require_once 'dbconfig.php';

    class 
    USER
    {    

        private 
    $conn;
        
        public function 
    __construct()
        {
            
    $database = new Database();
            
    $db $database->dbConnection();
            
    $this->conn $db;
        }
        
        public function 
    runQuery($sql)
        {
            
    $stmt $this->conn->prepare($sql);
            return 
    $stmt;
        }
        
        public function 
    lasdID()
        {
            
    $stmt $this->conn->lastInsertId();
            return 
    $stmt;
        }
        
        public function 
    register($uname,$email,$upass,$code)
        {
            try
            {                            
                
    $password md5($upass);
                
    $stmt $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass,tokenCode) 
                                                             VALUES(:user_name, :user_mail, :user_pass, :active_code)"
    );
                
    $stmt->bindparam(":user_name",$uname);
                
    $stmt->bindparam(":user_mail",$email);
                
    $stmt->bindparam(":user_pass",$password);
                
    $stmt->bindparam(":active_code",$code);
                
    $stmt->execute();    
                return 
    $stmt;
            }
            catch(
    PDOException $ex)
            {
                echo 
    $ex->getMessage();
            }
        }
        
        public function 
    login($email,$upass)
        {
            try
            {
                
    $stmt $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
                
    $stmt->execute(array(":email_id"=>$email));
                
    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
                
                if(
    $stmt->rowCount() == 1)
                {
                    if(
    $userRow['userStatus']=="Y")
                    {
                        if(
    $userRow['userPass']==md5($upass))
                        {
                            
    $_SESSION['userSession'] = $userRow['userID'];
                            return 
    true;
                        }
                        else
                        {
                            
    header("Location: index.php?error");
                            exit;
                        }
                    }
                    else
                    {
                        
    header("Location: index.php?inactive");
                        exit;
                    }    
                }
                else
                {
                    
    header("Location: index.php?error");
                    exit;
                }        
            }
            catch(
    PDOException $ex)
            {
                echo 
    $ex->getMessage();
            }
        }
        
        
        public function 
    is_logged_in()
        {
            if(isset(
    $_SESSION['userSession']))
            {
                return 
    true;
            }
        }
        
        public function 
    redirect($url)
        {
            
    header("Location: $url");
        }
        
        public function 
    logout()
        {
            
    session_destroy();
            
    $_SESSION['userSession'] = false;
        }
        
        function 
    send_mail($email,$message,$subject)
        {                        
            require_once(
    'mailer/class.phpmailer.php');
            
    $mail = new PHPMailer();
            
    $mail->IsSMTP(); 
            
    $mail->SMTPDebug  0;                     
            
    $mail->SMTPAuth   true;                  
            
    $mail->SMTPSecure "ssl";                 
            
    $mail->Host       "smtp.gmail.com";      
            
    $mail->Port       465;             
            
    $mail->AddAddress($email);
            
    $mail->Username="your_gmail_id_here@gmail.com";  
            
    $mail->Password="your_gmail_password_here";            
            
    $mail->SetFrom('your_gmail_id_here@gmail.com','Coding Cage');
            
    $mail->AddReplyTo("your_gmail_id_here@gmail.com","Coding Cage");
            
    $mail->Subject    $subject;
            
    $mail->MsgHTML($message);
            
    $mail->Send();
        }    
    }
    anyway i will keep on trying to fix the errors ...

    i hope all of these works someday ...

  18. #10
  19. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,113
    Rep Power
    2010
    Have you got it to work?

    Comments on this post

    • digimon agrees
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    MrFujin ,

    sorry for the late reply ..i was busy with something else ...

    no i cant get this to work ...

    Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\Apache24\htdocs\class.user.php:19 Stack trace: #0 C:\Apache24\htdocs\signup.php(20): USER->runQuery('SELECT * FROM t...') #1 {main} thrown in C:\Apache24\htdocs\class.user.php on line 19
    for some reason i cannot get past this error ...
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,515
    Rep Power
    1426
    Originally Posted by digimon
    what is the differences between these two files ..??
    The database credentials, for one thing. You're using a different password and your mysqli attempt does not try to access the dbtest database, while the pdo attempt does.

    Regards, Jens
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    75
    Rep Power
    4
    Browsing to the dbconfig.php file would only tell you useful information if there was a syntax error in the file. Since there's no code being executed in the file, you won't normally get any output from that file. The file only contains the class definition.

    The latest code you posted, reply #9, for dbconfig.php doesn't have the change that was given in reply #6. This would have halted execution if there is a connection error and prevented any follow-on errors, which is what you are getting now. The error, when you try to execute a query, isn't the cause of the problem, it's just a symptom at the tail end of the problem.

    I'm betting that when you browse to the signup.php page, you will see some type of PDO error near the top of the page concerning the connection failing. You may need to look in the 'view source' of the page if the styling for that page is dark on dark or low contrast colors.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    75
    Rep Power
    4
    It would appear that you have placed the php code after the end of the html document in all the files, not before, as it was originally written. This won't produce the result you expect and won't work correctly for the $msg output or the session_start()/header() statements.

    Beyond that, the tutorial you found has a number of problems and you should not try to use it or learn any (bad) practices from it. The biggest problem with it is in the user class definition. The user class should not be where the database connection is created at (your main application code should be responsible for creating the database connection) and it should only contain class methods that are user related. Doing a redirect and sending email are not user methods. Also, if the user class was designed and used correctly, there would be NO query directly referencing tbl_user in the main application logic. This class looks like it is just a collection of procedural code that has had a class definition wrapped around it.
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    4
    Originally Posted by JClasen
    The database credentials, for one thing. You're using a different password and your mysqli attempt does not try to access the dbtest database, while the pdo attempt does.

    Regards, Jens
    thanks JCLasen ,



    There are (more than) three popular ways to use MySQL from PHP.

    (DEPRECATED) The mysql functions are procedural and use manual escaping.
    mysqli is a replacement for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements.
    PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.

    I would recommend using PDO with prepared statements. It is a well-designed API and will let you more easily move to another database (including any that supports ODBC) if necessary.





    Originally Posted by DSmabismad
    Browsing to the dbconfig.php file would only tell you useful information if there was a syntax error in the file. Since there's no code being executed in the file, you won't normally get any output from that file. The file only contains the class definition.

    The latest code you posted, reply #9, for dbconfig.php doesn't have the change that was given in reply #6. This would have halted execution if there is a connection error and prevented any follow-on errors, which is what you are getting now. The error, when you try to execute a query, isn't the cause of the problem, it's just a symptom at the tail end of the problem.

    I'm betting that when you browse to the signup.php page, you will see some type of PDO error near the top of the page concerning the connection failing. You may need to look in the 'view source' of the page if the styling for that page is dark on dark or low contrast colors.
    Originally Posted by DSmabismad
    It would appear that you have placed the php code after the end of the html document in all the files, not before, as it was originally written. This won't produce the result you expect and won't work correctly for the $msg output or the session_start()/header() statements.

    Beyond that, the tutorial you found has a number of problems and you should not try to use it or learn any (bad) practices from it. The biggest problem with it is in the user class definition. The user class should not be where the database connection is created at (your main application code should be responsible for creating the database connection) and it should only contain class methods that are user related. Doing a redirect and sending email are not user methods. Also, if the user class was designed and used correctly, there would be NO query directly referencing tbl_user in the main application logic. This class looks like it is just a collection of procedural code that has had a class definition wrapped around it.

    DSmabismad ,

    thanks a lot for the replies ...

    i am trying it again with the original configurations ...

    it starts with a very weird error message on the top of the page ...

    first i thought it was some sort of weird comment ...

    when i open the ...

    i see this error .. i am not sure if its an error or a comment ...

    Connection error: could not find driver
    i wonder what that means ...

    let me try few more times ... and see if i can get a positive result ...
    Last edited by digimon; August 11th, 2016 at 10:33 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo