I'm new to PHP and was given a code snippet that looks like this:

{
    $DB = new DB('xxxxxx','oooooo','gggggg');
    $return_result = false;


    $this->clear_data();

    $m = new xmlrpcmsg(
            'ldapxml.authorize'
            , array(
                    new xmlrpcval($user_name, "string")
                    , new xmlrpcval($password, "string")
            )
    );

    // this is just a temp server for now
    $c = new xmlrpc_client(XMLRPC_CLIENT_PATH, XMLRPC_CLIENT_SERVER, XMLRPC_CLIENT_PORT);

    $c->return_type = 'phpvals';

    $r = $c->send($m);
    if ( !$r->faultCode() )
    {
        $v = $r->value();

        if ( $v )
        {

            $return_result = true;              

            $this->put('username', $user_name);
            $this->put('IP', $_SERVER['REMOTE_ADDR']);
            $this->put($this->_valid_session_flag, 1);

            // grab the users info

            $m = new xmlrpcmsg(
                    'ldapxml.getAttibutes'
                    , array(
                            new xmlrpcval($user_name, "string")
                            , new xmlrpcval($password, "string")
                    )
            );

            $query = "SELECT email FROM auth WHERE email = '".$email."'";
            $result = mydb::cxn()->query($query);
            if ($result != $user_name)
            {
                echo '<p style="color: red;">There was an error logging in...</p>';
            }

            $c = new xmlrpc_client(XMLRPC_CLIENT_PATH, XMLRPC_CLIENT_SERVER, XMLRPC_CLIENT_PORT);

            $c->return_type = 'phpvals';

            $r = $c->send($m);
            if ( !$r->faultCode() )
            {
                $v = $r->value();

                if ( $v )
                {
                    $this->put('title', $v['title']);
                    $this->put('email', $v['mail']);
                }
            }
            //}
        }
        else {
            echo '<p style="color: red;">There was an error logging in...</p>';

        }
    }

    return $return_result

What does this code do? Can someone break it down for me, line by line and helpt me wrap my head around it? Will it work?

Recommended Answers

All 9 Replies

I am still finding my way with OOP, but it looks like a base login validater class/function that creates a database connection and validates against the form input, although this is only a fraction of the class.

Why do you say it's a fragment?

Take line 62 of the code above:

$this->put('title', $v['title']);

It is a call to a function with in a class that has either been missed on the above snippet, or esides within a different function, but in the same call.

Have a look at the php.net example 1 here: example

This should help to clarify.

!!EDIT!!

Sorry I meant line 63

Sorry for the silly question but you mean the "put" part of it right?

    function put ( $data_name, $data )
    {
        $return_result = true;
        if ( isset($data_name) && isset($data) )
        {
            $_SESSION[$data_name] = $data;
        } else {
            $return_result = false;
        }
        return $return_result;
    }

you mean the "put" part of it right?

Correct.

Is this your coding? Or are you running through a tutorial?

Not a tutorial, justa personal thing. It's supposed to grab a username, then append "@gmail.com" to the username to compare it to a database table of e-mails and passwords.

I can't see any super obvious errors though.

function put ( $data_name, $data )
{
    $return_result = true;
    if ( isset($data_name) && isset($data) )
    {
        $_SESSION[$data_name] = $data;
    } else {
        $return_result = false;
    }
    return $return_result;
}           

function login( $user_name, $password )
{
    //Take username and password
    //Append @gmail to username to make username value = to an email
    $user_name = $user_name .= "@gmail.com";

    $user_name = strtolower($user_name); //Make username case INSENSITIVE (password is still case sensitive)

    $return_result = false;

    $m = new xmlrpcmsg(
            'ldapxml.authorize'
            , array(
                    new xmlrpcval($user_name, "string")
                    , new xmlrpcval($password, "string")
            )
    );

    //temp server
    $c = new xmlrpc_client(XMLRPC_CLIENT_PATH, XMLRPC_CLIENT_SERVER, XMLRPC_CLIENT_PORT);

    $c->return_type = 'phpvals';

    $r = $c->send($m);
    if ( !$r->faultCode() )
    {
        $v = $r->value();

        if ( $v )
        {
            $query = "SELECT email FROM auth WHERE email = '".$email."'";
            $result = mydb::cxn()->query($query);

            if ($result != $user_name)
            {
                echo '<p style="color: red;">There was an error logging in</p>';
            }

            else {

            $return_result = true;              

            $this->put('username', $user_name);
            $this->put('IP', $_SERVER['REMOTE_ADDR']);
            $this->put($this->_valid_session_flag, 1);

            // grab the users info
            $m = new xmlrpcmsg(
                    'ldapxml.getAttibutes'
                    , array(
                            new xmlrpcval($user_name, "string")
                            , new xmlrpcval($password, "string")
                    )
            );

            $c = new xmlrpc_client(XMLRPC_CLIENT_PATH, XMLRPC_CLIENT_SERVER, XMLRPC_CLIENT_PORT);

            $c->return_type = 'phpvals';

            $r = $c->send($m);
            if ( !$r->faultCode() )
            {
                $v = $r->value();

                if ( $v )
                {
                    $this->put('title', $v['title']);
                    $this->put('email', $v['mail']);
                }
            }
        }
        }
    }

    return $return_result;  
}

Ok then, lets start again, as you have now changed the coding to what to had put in your first POST.

Are you getting an error?

No errors at all in Eclipse.

It should take the user's input and check their username and password on the lrpc server, then add "@gmail.com" to it and check if the user exists in the auth table. So it should use the lrpc server to log you in and then the generated username + email to assign you a user role (like admin, member, etc.) based on the one set for you in the database.

So basically if your username is Squidge and your password is XXX then that should log you in, then come back and make you Squidge@gmail.com, look for your email in the auth table and see that you're an admin, then the content available to you on the site is that of an admin.

Definitely not the best way to do it, but it's the way it's got to be. Has to be some logical error...

$user_name = $user_name .= "@gmail.com";

Try changing this line [17] to :

$user_name = $user_name ."@gmail.com";

Then check $user_name before and after using var_dump to insure it has filled

Also try this function function login( $user_name, $password ) with hard entered data to make sure

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.