Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Login if condition problem

    Good day!

    I felt difficulties today because my boss told me that I should separate my html and php code and I should use template to call my html code and used function in template. In opendb function they used mysql_fetch_array, in my old code I used mysql_num_rows how can I adopt the function of opendb for my if condition

    here is my code:
    PHP Code:
    <?php
    error_reporting
    (E_ERROR E_WARNING E_PARSE);
    include(
    'includes/config.sender.php');
    include(
    'includes/template.inc');


    session_start();

      if (isset(
    $_SESSION['logged_in'])) {
         
    header('Location:machine1.php');
         die();
      }


     if (isset(
    $_POST['submit'])) {
        
    $username=$_POST['username']; 
        
    $password=$_POST['password'];
        
        
    $username addslashes_gpc($username);
        
    $password addslashes_gpc($password);
        
        
        
    $sql_select "SELECT
                        username,
                        password
                   FROM
                        machine_problem_rhoda_user
                   WHERE
                           username='$username'
                        AND
                        password='$password'
                        "
    ;
                        
        
    $result $_DB->opendb($sql_select);
        
        
    var_dump($result);
        
        
        
    //var_dump($sql_select);

        //$result=mysql_query($sql_select);
        //$count = $_DB->countdata($result
        
        //$count=mysql_num_rows($result);
    //as I sain in opendb them used mysql_fetch_array now my problem is what should i put in my if condition and also on my var_dump($result) the output is array(),it means it cannot get or see the data in my database?

        
    if($count==1){  
            
    $_SESSION['logged_in'] = true;
            
    header("location:machine1.php");
        }
        else {
        echo 
    "<center>";
        echo 
    "Wrong Username or Password";
        echo 
    "</center>";
        }
    }

    $tpl = new Template('.''keep');
    $tpl->set_file(array('handle' => 'html/index.html'));
    $tpl->parse('handle', array('handle'));
    $tpl->p('handle');
    ?>
    I'm sorry, but the template is not my code and i dont have the rights to change it.

    I hope somebody can help me.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Really we need to see what opendb is doing but you've not included that code.

    You say you used mysql_num_rows and now you must use mysql_fetch_array because you've been instructed to use a template. The template should make no difference. What those 2 functions do are completely different and until you show us more source code there is no way we can know what you are trying to do.

    FYI your boss is correct to ask you to seperate html from php. It results in much cleaner code and a much better and clearer way of thinking. I was working on someones php last night via TeamViewer and all i can say is that it wasn't very tidy. He was referring to the session all over the place then he'd be pulling variables out of a database then back to the session looking for a variable that hadn't been set... yuck. Make the change and be happy you have. Seperate logic and the use of templates is far superior to a mixed script.
    I can't really think of anything to write here now...

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I attach the code for function opendb.
    Attached Files Attached Files

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    From opendb.txt:
    PHP Code:
       function opendb ($query) {
         
    # connect if there's no active connection
         
    if ($this->_active_con == null$this->dbconnect();

         if ((
    $result mysql_query($query))==false)
            
    $this->_safe_croak("query error: " mysql_error(), $querytrue);
         
    $alldata = array();
         
    $dline 0;
         while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
            foreach (
    $line as $col_name => $col_value) {
               
    $alldata[$dline]["$col_name"] = "$col_value";
            }
            
    $dline $dline 1;
         }
         
    mysql_free_result($result);
         
    $this->_data_bucket $alldata;
         
    $this->_last_d 0;
         return(
    $alldata);
       } 
    So opendb() returns an array of results?

    Why not use count(opendb($Query))?

    PHP Code:
        $sql_select "SELECT
                        username,
                        password
                   FROM
                        machine_problem_rhoda_user
                   WHERE
                           username='$username'
                        AND
                        password='$password'
                        "
    ;
                        
        
    $count count($_DB->opendb($sql_select)); 
    That way you make minimal changes to your code but it should still work.
    I can't really think of anything to write here now...

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    From opendb.txt:
    PHP Code:
       function opendb ($query) {
         
    # connect if there's no active connection
         
    if ($this->_active_con == null$this->dbconnect();

         if ((
    $result mysql_query($query))==false)
            
    $this->_safe_croak("query error: " mysql_error(), $querytrue);
         
    $alldata = array();
         
    $dline 0;
         while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
            foreach (
    $line as $col_name => $col_value) {
               
    $alldata[$dline]["$col_name"] = "$col_value";
            }
            
    $dline $dline 1;
         }
         
    mysql_free_result($result);
         
    $this->_data_bucket $alldata;
         
    $this->_last_d 0;
         return(
    $alldata);
       } 
    So opendb() returns an array of results?

    Why not use count(opendb($Query))?

    PHP Code:
        $sql_select "SELECT
                        username,
                        password
                   FROM
                        machine_problem_rhoda_user
                   WHERE
                           username='$username'
                        AND
                        password='$password'
                        "
    ;
                        
        
    $count count($_DB->opendb($sql_select)); 
    That way you make minimal changes to your code but it should still work.
    I try this code:
    PHP Code:
    <?php
    error_reporting
    (E_ERROR E_WARNING E_PARSE);
    include(
    'includes/config.sender.php');
    include(
    'includes/template.inc');


    session_start();

      if (isset(
    $_SESSION['logged_in'])) {
         
    header('Location:machine1.php');
         die();
      }


     if (isset(
    $_POST['submit'])) {
        
    $username=$_POST['username']; 
        
    $password=$_POST['password'];
        
        
    $username addslashes_gpc($username);
        
    $password addslashes_gpc($password);
        
        
         
    $sql_select "SELECT
                        username,
                        password
                   FROM
                        machine_problem_rhoda_user
                   WHERE
                           username='$username'
                        AND
                        password='$password'
                        "
    ;
                        
        
    $count count($_DB->opendb($sql_select)); 
        
        
    //var_dump($result);
        
    print_r($result);
        
        
    //var_dump($sql_select);

        //$result=mysql_query($sql_select);
        //$count = $_DB->countdata($result
        
        //$count=mysql_num_rows($result);

        
    if($count==1){  
            
    $_SESSION['logged_in'] = true;
            
    header("location:machine1.php");
        }
        else {
        echo 
    "<center>";
        echo 
    "Wrong Username or Password";
        echo 
    "</center>";
        }
    }








    $tpl = new Template('.''keep');
    $tpl->set_file(array('handle' => 'html/index.html'));
    $tpl->parse('handle', array('handle'));
    $tpl->p('handle');
    ?>
    and when I run my code when I input correct username and password(my password is already encrypted) and when I submit the output display is wrong username or password:confused::(

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    So your password in the database is already encrypted.. but your not encrypted the password from the form that the user has input?

    Also what does that print_r function output?
    I can't really think of anything to write here now...

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    So your password in the database is already encrypted.. but your not encrypted the password from the form that the user has input?

    Also what does that print_r function output?
    Yes, kindly help me to solved my problem..I really don't know what to do..I need to solved it today..:(

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    You're being very cryptic.

    Yes what? - Yes your ARE encrypting your password or Yes you are not as i have suggested?

    I have asked you to tell me what print_r produces and you have ignored me. If you do not help me to help you then you will not get it fixed. If you won't tell me then you have a bigger problem than your php code.

    Also you're running this same thread on sitepoint. The more modifications you make to your code the harder it will be to follow over two websites.
    I can't really think of anything to write here now...

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    You're being very cryptic.

    Yes what? - Yes your ARE encrypting your password or Yes you are not as i have suggested?

    I have asked you to tell me what print_r produces and you have ignored me. If you do not help me to help you then you will not get it fixed. If you won't tell me then you have a bigger problem than your php code.

    Also you're running this same thread on sitepoint. The more modifications you make to your code the harder it will be to follow over two websites.
    Yes I encrypt my password using phpmyadmin and I copied the encrypted password to my database in slqyog.

    this is the result of print_r($sql_select);
    SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' 0

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    But where in the code you have provided, are you encrypting the password from the submitted form? - I will not ask you this again.

    I do not care how you encrypted it and put it into the database, I want to know where you are encrypting it from the user login form because I can not see you doing this in your code.
    I can't really think of anything to write here now...

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by newphpcoder View Post
    PHP Code:
        $count count($_DB->opendb($sql_select)); 
        
        
    //var_dump($result);
        
    print_r($result); //This is the only print_r in your code. 
    Quote Originally Posted by tangoforce View Post
    Also what does that print_r function output?
    Quote Originally Posted by tangoforce View Post
    I have asked you to tell me what print_r produces and you have ignored me. If you do not help me to help you then you will not get it fixed. If you won't tell me then you have a bigger problem than your php code.
    Quote Originally Posted by newphpcoder View Post
    this is the result of print_r($sql_select);
    SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' 0
    You see, you are deliberately ignoring my requests for information. I have asked you twice for the output from the line with print_r and you ignored me the first time and the second time you gave me the print_r of a different variable.

    Why are you asking for help if you do not want to be helped?
    I can't really think of anything to write here now...

  • #12
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    But where in the code you have provided, are you encrypting the password from the submitted form? - I will not ask you this again.

    I do not care how you encrypted it and put it into the database, I want to know where you are encrypting it from the user login form because I can not see you doing this in your code.
    I encrypt my password in phpmyadmin...

  • #13
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    You see, you are deliberately ignoring my requests for information. I have asked you twice for the output from the line with print_r and you ignored me the first time and the second time you gave me the print_r of a different variable.

    Why are you asking for help if you do not want to be helped?
    this is the result of print_r

    SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' 0

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by newphpcoder View Post
    PHP Code:
        print_r($result); 
    Quote Originally Posted by newphpcoder View Post
    this is the result of print_r($sql_select);
    Why are you giving me the print_r of $sql_select again?

    For the very last time, what is the print_r of $result?

    If you do not give me a satisfactory answer I will ask a moderator to lock this topic.
    I can't really think of anything to write here now...

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Deleted.
    I can't really think of anything to write here now...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •