SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert login function to php4 (was "help with php coding")

    I want to convert this function to php4: PLEASE HELP !!

    function login($username, $password)
    {
    $conn= db_connect();

    $query = "select * from user
    where username='$username'
    and password = sha1('$password')";

    $result = mysql_query($query);
    if (!$result)
    die ('Could not log you in.');

    if ($result->num_rows($result)>0)
    return true;
    else
    die ('Could not log you in.');
    }
    Also in php5 there is an try catch function , does anyone know the alternative for php4

    Thanks a lot

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try/catch are exception handling constructs, and PHP4 doesn't have exceptions.
    Last edited by Dan Grossman; Jul 7, 2007 at 14:54.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to connect using something like :
    PHP Code:
    function db_open() {
        
    $db mysql_connect('localhost'$user_name$password);
        if (!
    $db) {
         die(
    'Could not connect: ' mysql_error());
        }
      
        
    mysql_select_db$Database_to_use );
        
        if (
    mysql_errno()) {
          echo 
    'Could not open data base<br>';
          echo 
    'Error Number : ' .mysql_errno();
          exit;
        } Else {
          return 
    $db;
        }

    The $user_name & $password in the mysql_connect function need to be for your data base not the user name & password you are trying to log in.

    PHP Code:
    function login($username$password) {
        
    $conndb_open();

        
    $query "select * from user where username='$username' and password = sha1('$password')";

        
    $result mysql_query($query);
        if (!
    $result) {
            return 
    FALSE;
        }
         return 
    TRUE;

    Post the exception handling try catch block and someone will re-write it for you.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your login function will always return true.

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have no returned resultset it will return false.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post
    If you have no returned resultset it will return false.
    No, mysql_query() only returns false on an error. A result set with zero rows is not an error, it is a valid result resource, and does not evaluate to boolean false.

    You need to either select the COUNT() of rows then retrieve that value, or check mysql_num_rows() on the result set.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for your help here is my whole login function which is written for php5 but I am using php4, need to convert this to php4, please help me ...................thanks once again,

    function login($username, $password)
    // check username and password with db
    // if yes, return true
    // else throw exception
    {
    // connect to db
    $conn = database_connect();

    // check if username is unique
    $result = $conn->query("select * from user
    where username='$username'
    and passwd = sha1('$password')");
    if (!$result)
    throw new Exception('Could not log you in.');

    if ($result->num_rows>0)
    return true;
    else
    throw new Exception('Could not log you in.');
    }

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I assume this database class you're using will still be around? In that case, all that needs to be changed is removing the exception. You could return false if you change the caller to expect that instead of catching an exception...

    PHP Code:
    function login($username$password) {

      
    $conn database_connect();
      
    $result $conn->query("select * from user
      where username='
    $username'
      and passwd = sha1('
    $password')");

      if (!
    $result || $result->num_rows == 0)
        return 
    false;
      return 
    true;



  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but it is giving me a error:


    Fatal error: Call to a member function on a non-object in /home/domsin/public_html/mydomain/new/user_fn.php on line 151
    and in line 151 it's this:
    $result = $conn->query("select * from user where username='$username' and passwd = sha1('$password')");


  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Then database_connect() isn't returning an object.. and that's outside the code you asked to change.

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, I don't understand what you are trying to say but I have listed the database function below: Thanks again !!

    function database_connect ()
    {
    // Connecting, selecting database
    $link = mysql_connect('localhost', 'username', 'password')
    or die('Could not connect: ' . mysql_error());
    echo '';
    mysql_select_db('database') or die('Could not select database');
    }
    but I can insert the user into table using php coe but couldn't login.

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please help me with the code above and I also want to convert this to php4.

    require_once('site_functions.php');
    session_start();

    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username && $password)
    {

    try
    {
    login($username, $password);
    $_SESSION['valid_user'] = $username;
    }
    catch(Exception $e)
    {
    display_header('Problem:');
    echo 'You could not be logged in.
    You must be logged in to view this page.';
    display_url('login.php', 'Login');
    display_footer();
    exit;
    }

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please please help, as I need as soon as possible.

    Thanks a lot...

  14. #14
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Why not just get a new PHP4 DB abstraction instead of trying to convert from PHP5?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  15. #15
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Why not just get a new PHP4 DB abstraction instead of trying to convert from PHP5?
    thanks and can you please extend your explanation with examples since I have just started programming.

    thanks

  16. #16
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    No, mysql_query() only returns false on an error. A result set with zero rows is not an error, it is a valid result resource, and does not evaluate to boolean false.

    You need to either select the COUNT() of rows then retrieve that value, or check mysql_num_rows() on the result set.
    That is an excellent point. Well done Dan.
    ------------------


Bookmarks

Posting Permissions

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