PHP Login System with Admin Features

This forum was created to talk about the PHP Login System with admin features created by jpmaster77 on evolt's website


    avatar system

    Share
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    avatar system

    Post  mattastic on Sun Oct 11, 2009 10:24 am

    anyone know how to make an avatar system for jp's script? like have it appear when you log in and on your user info and where it shows when your online? maybe a gallery/album feature to go along with it?
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Mon Oct 12, 2009 2:51 am

    another question - i got an avatar system working from this page http://evolt.org/PHP-Login-System-with-Admin-Features?from=1300&comments_per_page=50 (its a post by bustya). the problem is i dont know how to resize the image when its uploaded, it appears big. how would i do this and make it look like a thumbnail?
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Tue Oct 13, 2009 11:49 am

    have you checked out ImageMagick or the GD libraries? Im pretty sure they are both capable of doing this. if you are not too comfortable with PHP's libraries i saw this short script from sitepoint here. Its definitely not the most powerful of images resizing scripts, but it can get the job done.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 1:17 am

    based on the avatar script from that bustya guy, where would i put sitepoint's script? i have no idea how to place it tbh.
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 9:35 am

    the thing with the sitepoint script is that it just tells the html what width and height it should display an image. this means if youve got a 1600 x 1280 image your browser will load the whole image, and then resize. This can mean slow load times...

    Of course you shouldnt run into this problem cause you check to make sure the image is less than 15KB, but i figured id just let you know anyway.

    Anyway, on to your question. You may want to consider putting the function in session.php because that page is included in all other pages (like userinfo.php). now for resizing the image, locate the snippet bustya gave us and edit it like this:
    Code:
    /* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
          if(strcmp($database->username,$req_user_info['avatar']) == 0){
    $mysock = getimagesize("include/userimg/no_avatar.gif");
    echo '<img src="include/userimg/no_avatar.gif" ' . imageResize($mysock[0], $mysock[1], 150) . '/>';
    }
    else{
    $mysock = getimagesize("include/userimg/" . $req_user_info['avatar']);
    echo '<img src="include/userimg/'.$req_user_info['avatar'].'" ' . imageResize($mysock[0], $mysock[1], 150) . '/>';
    }

    this will resize a users avatar as well as your no_avatar.gif file. Im assuming you read the sitepoint script and know how it works so dont forget to change your target to find the results youre looking for.

    let me know how this works for you.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 11:46 am

    i replaced the code with the one you gave me and i get this when i go to userinfo.php

    Fatal error: Call to undefined function imageresize() in /home/vol11/xtreemhost.com/xth_4280991/htdocs/userinfo.php on line 49

    i know i have to do more with it just dont know what, and i'm not sure what to change the target to either Neutral .
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 11:56 am

    Well first off target is entirely up to you. Experiment with some values till you get what you want. The error you are getting is cause your script can't find the function you are calling. Did you put it in session.php? Can you post where you put it? If you put it somewhere else you have to make sure you include it in userunfo.php.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 12:42 pm

    here is my userinfo.php. i only put it in this php.

    Code:
    <?
    /**
     * UserInfo.php
     *
     * This page is for users to view their account information
     * with a link added for them to edit the information.
     *
     * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
     * Last Updated: August 26, 2004
     */
    include("include/session.php");
    ?>

    <html>
    <title>Jpmaster77's Login Script</title>
    <body>

    <?
    /* Requested Username error checking */
    $req_user = trim($_GET['user']);
    if(!$req_user || strlen($req_user) == 0 ||
      !eregi("^([0-9a-z])+$", $req_user) ||
      !$database->usernameTaken($req_user)){
      die("Username not registered");
    }

    /* Logged in user viewing own account */
    if(strcmp($session->username,$req_user) == 0){
      echo "<h1>My Account</h1>";
    }
    /* Visitor not viewing own account */
    else{
      echo "<h1>User Info</h1>";
    }

    /* Display requested user information */
    $req_user_info = $database->getUserInfo($req_user);

    /* Username */
    echo "<b>Username: ".$req_user_info['username']."</b><br>";

    /* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
          if(strcmp($database->username,$req_user_info['avatar']) == 0){
    $mysock = getimagesize("include/userimg/no_avatar.gif");
    echo '<img src="include/userimg/no_avatar.gif" ' . imageResize($mysock[0], $mysock[1], 150) . '/>';
    }
    else{
    $mysock = getimagesize("include/userimg/" . $req_user_info['avatar']);
    echo '<img src="include/userimg/'.$req_user_info['avatar'].'" ' . imageResize($mysock[0], $mysock[1], 150) . '/>';
    }

    /* Firstname */
    echo "<b>Firstname:</b> ".$req_user_info['firstname']."<br>";

    /* Lastname */
    echo "<b>Lastname:</b> ".$req_user_info['lastname']."<br>";

    /* Country */
    foreach (countryArray() as $key => $value) if($req_user_info['country'] == $key) $country = $value;
    echo "<b>Country:</b> ".$country."<br>";

    /* Email */
    echo "<b>Email:</b> ".$req_user_info['email']."<br>";

    /**
     * Note: when you add your own fields to the users table
     * to hold more information, like homepage, location, etc.
     * they can be easily accessed by the user info array.
     *
     * $session->user_info['location']; (for logged in users)
     *
     * ..and for this page,
     *
     * $req_user_info['location']; (for any user)
     */

    /* If logged in user viewing own account, give link to edit */
    if(strcmp($session->username,$req_user) == 0){
      echo "<br><a href=\"useredit.php\">Edit Account Information</a><br>";
    }

    /* Link back to main */
    echo "<br>Back To [<a href=\"index.php\">Main</a>]<br>";

    ?>

    </body>
    </html>
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 1:32 pm

    alright, but the actual function from sitepoint, did you add that into your code anywhere? thats why you get that error, because your script cant find the function.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 1:46 pm

    you add this to session.php right? if i do, do i have to change anything in this code?

    Code:

    function imageResize($width, $height, $target) {

    //takes the larger size of the width and height and applies the 
    formula accordingly...this is so this script will work 
    dynamically with any size image

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    //returns the new sizes in html image tag format...this is so you
    can plug this function inside an image tag and just get the

    return "width=\"$width\" height=\"$height\"";

    }
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 1:49 pm

    you can really add the function to where ever you want. I just chose session because every page in your site would most likely be including session.php. that means which ever pages are including session.php can also use your image resizing function.

    where ever you choose to add the function, you shouldnt need to change anything inside of it.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 2:10 pm

    reason why i asked if i should change anything because no matter what way i add it i get this: Parse error: syntax error, unexpected T_STRING in /home/vol11/xtreemhost.com/xth_4280991/htdocs/include/session.php on line 494

    i added it where it says "//img resize"

    Code:

    <?
    /**
     * Session.php
     *
     * The Session class is meant to simplify the task of keeping
     * track of logged in users and also guests.
     *
     * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
     * Last Updated: August 19, 2004
     */
    include("database.php");
    include_once('function.php');
    include("mailer.php");
    include("form.php");


    class Session
    {
      var $username;    //Username given on sign-up
      var $userid;      //Random value generated on current login
      var $userlevel;    //The level to which the user pertains
      var $time;        //Time user was last active (page loaded)
      var $logged_in;    //True if user is logged in, false otherwise
      var $userinfo = array();  //The array holding all user info
      var $url;          //The page url current being viewed
      var $referrer;    //Last recorded site page viewed
      /**
        * Note: referrer should really only be considered the actual
        * page referrer in process.php, any other time it may be
        * inaccurate.
        */

      /* Class constructor */
      function Session(){
          $this->time = time();
          $this->startSession();
      }

      /**
        * startSession - Performs all the actions necessary to
        * initialize this session object. Tries to determine if the
        * the user has logged in already, and sets the variables
        * accordingly. Also takes advantage of this page load to
        * update the active visitors tables.
        */
      function startSession(){
          global $database;  //The database connection
          session_start();  //Tell PHP to start the session

          /* Determine if user is logged in */
          $this->logged_in = $this->checkLogin();

          /**
          * Set guest value to users not logged in, and update
          * active guests table accordingly.
          */
          if(!$this->logged_in){
            $this->username = $_SESSION['username'] = GUEST_NAME;
            $this->userlevel = GUEST_LEVEL;
            $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
          }
          /* Update users last active timestamp */
          else{
            $database->addActiveUser($this->username, $this->time);
          }
         
          /* Remove inactive visitors from database */
          $database->removeInactiveUsers();
          $database->removeInactiveGuests();
         
          /* Set referrer page */
          if(isset($_SESSION['url'])){
            $this->referrer = $_SESSION['url'];
          }else{
            $this->referrer = "/";
          }

          /* Set current url */
          $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
      }

      /**
        * checkLogin - Checks if the user has already previously
        * logged in, and a session with the user has already been
        * established. Also checks to see if user has been remembered.
        * If so, the database is queried to make sure of the user's
        * authenticity. Returns true if the user has logged in.
        */
      function checkLogin(){
          global $database;  //The database connection
          /* Check if user has been remembered */
          if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
            $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
            $this->userid  = $_SESSION['userid']  = $_COOKIE['cookid'];
          }

          /* Username and userid have been set and not guest */
          if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
            $_SESSION['username'] != GUEST_NAME){
            /* Confirm that username and userid are valid */
            if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
                /* Variables are incorrect, user not logged in */
                unset($_SESSION['username']);
                unset($_SESSION['userid']);
                return false;
            }

            /* User is logged in, set class variables */
            $this->userinfo  = $database->getUserInfo($_SESSION['username']);
            $this->username  = $this->userinfo['username'];
            $this->userid    = $this->userinfo['userid'];
            $this->userlevel = $this->userinfo['userlevel'];
            return true;
          }
          /* User not logged in */
          else{
            return false;
          }
      }

      /**
        * login - The user has submitted his username and password
        * through the login form, this function checks the authenticity
        * of that information in the database and creates the session.
        * Effectively logging in the user if all goes well.
        */
      function login($subuser, $subpass, $subremember){
          global $database, $form;  //The database and form object

          /* Username error checking */
          $field = "user";  //Use field name for username
          if(!$subuser || strlen($subuser = trim($subuser)) == 0){
            $form->setError($field, "* Username not entered");
          }
          else{
            /* Check if username is not alphanumeric */
            if(!eregi("^([0-9a-z])*$", $subuser)){
                $form->setError($field, "* Username not alphanumeric");
            }
          }

          /* Password error checking */
          $field = "pass";  //Use field name for password
          if(!$subpass){
            $form->setError($field, "* Password not entered");
          }
         
          /* Return if form errors exist */
          if($form->num_errors > 0){
            return false;
          }

          /* Checks that username is in database and password is correct */
          $subuser = stripslashes($subuser);
          $result = $database->confirmUserPass($subuser, md5($subpass));

          /* Check error codes */
          if($result == 1){
            $field = "user";
            $form->setError($field, "* Username not found");
          }
          else if($result == 2){
            $field = "pass";
            $form->setError($field, "* Invalid password");
          }
         
          /* Return if form errors exist */
          if($form->num_errors > 0){
            return false;
          }

          /* Username and password correct, register session variables */
          $this->userinfo  = $database->getUserInfo($subuser);
          $this->username  = $_SESSION['username'] = $this->userinfo['username'];
          $this->userid    = $_SESSION['userid']  = $this->generateRandID();
          $this->userlevel = $this->userinfo['userlevel'];
         
          /* Insert userid into database and update active users table */
          $database->updateUserField($this->username, "userid", $this->userid);
          $database->addActiveUser($this->username, $this->time);
          $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);

          /**
          * This is the cool part: the user has requested that we remember that
          * he's logged in, so we set two cookies. One to hold his username,
          * and one to hold his random value userid. It expires by the time
          * specified in constants.php. Now, next time he comes to our site, we will
          * log him in automatically, but only if he didn't log out before he left.
          */
          if($subremember){
            setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
            setcookie("cookid",  $this->userid,  time()+COOKIE_EXPIRE, COOKIE_PATH);
          }

          /* Login completed successfully */
          return true;
      }

      /**
        * logout - Gets called when the user wants to be logged out of the
        * website. It deletes any cookies that were stored on the users
        * computer as a result of him wanting to be remembered, and also
        * unsets session variables and demotes his user level to guest.
        */
      function logout(){
          global $database;  //The database connection
          /**
          * Delete cookies - the time must be in the past,
          * so just negate what you added when creating the
          * cookie.
          */
          if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
            setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
            setcookie("cookid",  "", time()-COOKIE_EXPIRE, COOKIE_PATH);
          }

          /* Unset PHP session variables */
          unset($_SESSION['username']);
          unset($_SESSION['userid']);

          /* Reflect fact that user has logged out */
          $this->logged_in = false;
         
          /**
          * Remove from active users table and add to
          * active guests tables.
          */
          $database->removeActiveUser($this->username);
          $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
         
          /* Set user level to guest */
          $this->username  = GUEST_NAME;
          $this->userlevel = GUEST_LEVEL;
      }

      /**
        * register - Gets called when the user has just submitted the
        * registration form. Determines if there were any errors with
        * the entry fields, if so, it records the errors and returns
        * 1. If no errors were found, it registers the new user and
        * returns 0. Returns 2 if registration failed.
        */
      function register($subavatar, $subuser, $subpass, $subconfpass, $subemail, $subfirstname, $sublastname, $subcountry, $accept_terms){
          global $database, $form, $mailer;  //The database, form and mailer object
         
          /* Username error checking */
          $field = "user";  //Use field name for username
          if(!$subuser || strlen($subuser = trim($subuser)) == 0){
            $form->setError($field, "* Username not entered");
          }
          else{
            /* Spruce up username, check length */
            $subuser = stripslashes($subuser);
            if(strlen($subuser) < 5){
                $form->setError($field, "* Username below 5 characters");
            }
            else if(strlen($subuser) > 30){
                $form->setError($field, "* Username above 30 characters");
            }
            /* Check if username is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", $subuser)){
                $form->setError($field, "* Username not alphanumeric");
            }
            /* Check if username is reserved */
            else if(strcasecmp($subuser, GUEST_NAME) == 0){
                $form->setError($field, "* Username reserved word");
            }
            /* Check if username is already in use */
            else if($database->usernameTaken($subuser)){
                $form->setError($field, "* Username already in use");
            }
            /* Check if username is banned */
            else if($database->usernameBanned($subuser)){
                $form->setError($field, "* Username banned");
            }
          }

          /* Password error checking */
          $field = "pass";  //Use field name for password
          if(!$subpass){
            $form->setError($field, "* Password not entered");
          }
          else{
            /* Spruce up password and check length*/
            $subpass = stripslashes($subpass);
            if(strlen($subpass) < 4){
                $form->setError($field, "* Password too short");
            }
            /* Check if password is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
                $form->setError($field, "* Password not alphanumeric");
            }
            /**
              * Note: I trimmed the password only after I checked the length
              * because if you fill the password field up with spaces
              * it looks like a lot more characters than 4, so it looks
              * kind of stupid to report "password too short".
              */
          }

          /* Email error checking */
          $field = "email";  //Use field name for email
          if(!$subemail || strlen($subemail = trim($subemail)) == 0){
            $form->setError($field, "* Email not entered");
          }
          else{
            /* Check if valid email address */
            $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                    ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                    ."\.([a-z]{2,}){1}$";
            if(!eregi($regex,$subemail)){
                $form->setError($field, "* Email invalid");
            }
            $subemail = stripslashes($subemail);
          }

      /*Firstname error checking */
    $field = "firstname";
    if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0) $form->setError($field, "* Firstname not entered");
    $subfirstname = stripslashes($subfirstname);

    /*Lastname error checking */
    $field = "lastname";
    if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0) $form->setError($field, "* Lastname not entered");
    $sublastname = stripslashes($sublastname);

    /*Country error checking*/
    $field = "country";     
    if($subcountry == '') $form->setError($field, "* Country Not Entered");

    /*Comfirm Password checking*/
    $field = "confpass";
    if(!$subconfpass){
        $form->setError($field, "* Please confirm you password");
    }
    else{
        if(strcmp($subconfpass, $subpass) != 0){
            $form->setError($field, "* Your passwords don't match");
        }
    }

    /* Accept Terms error checking */
    $field = "accept_terms"; 
    if($accept_terms != 1){
      $form->setError($field, "* You must accept the terms to register");
    }

          /* Errors exist, have user correct them */
          if($form->num_errors > 0){
            return 1;  //Errors with form
          }
          /* No errors, add the new account to the */
          else{
            if($database->addNewUser($subavatar, $subuser, md5($subpass), $subemail, $subfirstname, $sublastname, $subcountry)){
                if(EMAIL_WELCOME){
                  $mailer->sendWelcome($subuser,$subemail,$subpass);
                }
                return 0;  //New user added succesfully
            }else{
                return 2;  //Registration attempt failed
            }
          }
      }

      /**
        * editAccount - Attempts to edit the user's account information
        * including the password, which it first makes sure is correct
        * if entered, if so and the new password is in the right
        * format, the change is made. All other fields are changed
        * automatically.
        */
      function editAccount($subcurpass, $subnewpass, $subemail, $subfirstname, $sublastname, $subcountry, $subavatar){
          global $database, $form;  //The database and form object
          /* New password entered */
          if($subnewpass){
            /* Current Password error checking */
            $field = "curpass";  //Use field name for current password
            if(!$subcurpass){
                $form->setError($field, "* Current Password not entered");
            }
            else{
                /* Check if password too short or is not alphanumeric */
                $subcurpass = stripslashes($subcurpass);
                if(strlen($subcurpass) < 4 ||
                  !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
                  $form->setError($field, "* Current Password incorrect");
                }
                /* Password entered is incorrect */
                if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
                  $form->setError($field, "* Current Password incorrect");
                }
            }
           
            /* New Password error checking */
            $field = "newpass";  //Use field name for new password
            /* Spruce up password and check length*/
            $subpass = stripslashes($subnewpass);
            if(strlen($subnewpass) < 4){
                $form->setError($field, "* New Password too short");
            }
            /* Check if password is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
                $form->setError($field, "* New Password not alphanumeric");
            }
          }
          /* Change password attempted */
          else if($subcurpass){
            /* New Password error reporting */
            $field = "newpass";  //Use field name for new password
            $form->setError($field, "* New Password not entered");
          }
         
          /* Email error checking */
          $field = "email";  //Use field name for email
          if($subemail && strlen($subemail = trim($subemail)) > 0){
            /* Check if valid email address */
            $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                    ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                    ."\.([a-z]{2,}){1}$";
            if(!eregi($regex,$subemail)){
                $form->setError($field, "* Email invalid");
            }
            $subemail = stripslashes($subemail);
          }
         
                /*Firstname error checking */
          $field = "firstname";
          if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0) $form->setError($field, "* Firstname not entered");
          $subfirstname = stripslashes($subfirstname);

                /*Lastname error checking */
     $field = "lastname";     
    if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0){       
      $form->setError($field, "* Lastname not entered");     
    }         
      $sublastname = stripslashes($sublastname);     

    /*Country error checking */
    $field = "country"; 
    if($subcountry == '') $form->setError($field, "* country not entered");

    $field = "avatar"; //Use field name for avatar
    //Check that we have a file
    if((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {
      //Check if the file is JPEG/GIF and it's size is less than 15Kb
      $filename = basename($_FILES['avatar']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);
      if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "gif") && ($_FILES["avatar"]["type"] == "image/jpeg") || ($_FILES["avatar"]["type"] == "image/pjpeg") || ($_FILES["avatar"]["type"] == "image/gif") &&
      //the size below this line states it must be smaller than 15Kb
        ($_FILES["avatar"]["size"] < 15000)) {
      //the line below this gives the uploaded file a random number name
    $random_digit=rand(0000,9999);

    //combine random digit to you file name to create new file name
    //use dot (.) to combile these two variables
    $newplus=$random_digit.".";
    $subavatar=$newplus.$ext;
            $subavatar = stripslashes($subavatar);

        //Determine the path to which we want to save this file
          $newname = dirname(__FILE__).'/userimg/'.$subavatar;
          //Check if the file with the same name is already exists on the server
          if (!file_exists($newname)) {
            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['avatar']['tmp_name'],$newname))) {
            $database->updateUserField($this->username,"avatar",$subavatar);
            } else {
              $form->setError($field, "* Error: A problem occurred during file upload!");
            }
          } else {
              $form->setError($field, "* Error: File ".$_FILES["avatar"]["name"]." already exists");
          }
      } else {
              $form->setError($field, "* Error: Only .jpg or .gif images please.");
      }
    } else {
             
    }


    //img resize
    function imageResize($width, $height, $target) {

    //takes the larger size of the width and height and applies the 
    formula accordingly...this is so this script will work 
    dynamically with any size image

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    //returns the new sizes in html image tag format...this is so you
    can plug this function inside an image tag and just get the

    return "width=\"$width\" height=\"$height\"";

    }


    /* Errors exist, have user correct them */
          if($form->num_errors > 0){
            return false;  //Errors with form
          }

          /* Update password since there were no errors */
          if($subcurpass && $subnewpass){
            $database->updateUserField($this->username,"password",md5($subnewpass));
          }
         
          /* Change Email */
          if($subemail){
            $database->updateUserField($this->username,"email",$subemail);
          }
         
          /* Change Firstname*/
        if($subfirstname){
        $database->updateUserField($this->username,"firstname",$subfirstname);
        }

        /* Change Lastname */
    if($sublastname){
    $database->updateUserField($this->username,"lastname",$sublastname);
    }

    /* Change Country */
    if($subcountry)
    $database->updateUserField($this->username,"country",$subcountry);



          /* Change Avatar (If the user is uploading a new avatar unlink will delete their old one from the directory "userimg".)*/
          if($subavatar){
    $avadel='include/userimg/'.$this->userinfo['avatar'];
          if (empty($this->userinfo['avatar'])) {$avadel='No Avatar to delete';}
            else
          {
    unlink($avadel);
          }
            $database->updateUserField($this->username,"avatar",$subavatar);
          }

          /* Success! */
          return true;
      }
     
      /**
        * isAdmin - Returns true if currently logged in user is
        * an administrator, false otherwise.
        */
      function isAdmin(){
          return ($this->userlevel == ADMIN_LEVEL ||
                  $this->username  == ADMIN_NAME);
      }
     
      /**
        * generateRandID - Generates a string made up of randomized
        * letters (lower and upper case) and digits and returns
        * the md5 hash of it to be used as a userid.
        */
      function generateRandID(){
          return md5($this->generateRandStr(16));
      }
     
      /**
        * generateRandStr - Generates a string made up of randomized
        * letters (lower and upper case) and digits, the length
        * is a specified parameter.
        */
      function generateRandStr($length){
          $randstr = "";
          for($i=0; $i<$length; $i++){
            $randnum = mt_rand(0,61);
            if($randnum < 10){
                $randstr .= chr($randnum+48);
            }else if($randnum < 36){
                $randstr .= chr($randnum+55);
            }else{
                $randstr .= chr($randnum+61);
            }
          }
          return $randstr;
      }
    };


    /**
     * Initialize session object - This must be initialized before
     * the form object because the form uses session variables,
     * which cannot be accessed unless the session has started.
     */
    $session = new Session;

    /* Initialize form object */
    $form = new Form;

    ?>
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 2:17 pm

    thats very odd. My first instinct is that because there arent any strings in this function this has something to do with the comments inside of the function. try putting the comments in a different type of comment syntax like this:

    Code:
    /* img resize */
    function imageResize($width, $height, $target) {

    /* takes the larger size of the width and height and applies the
    formula accordingly...this is so this script will work
    dynamically with any size image */

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    /* gets the new value and applies the percentage, then rounds the value */
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    /* returns the new sizes in html image tag format...this is so you
    can plug this function inside an image tag and just get the */

    return "width=\"$width\" height=\"$height\"";

    }


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 2:42 pm

    i think it was the comments because the page changed after i added that but now i get this again Fatal error: Call to undefined function imageresize() in /home/vol11/xtreemhost.com/xth_4280991/htdocs/userinfo.php on line 49.
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 2:44 pm

    alright so we're getting closer.

    functions are case sensitive. in the code we call the function imageResize and the error you pasted says imageresize so see if that is our problem.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 3:05 pm

    in the error the 'r' is lowercase but when i check where the error is its uppercase. should all the "r's" in "imageresize" be lower case or uppercase?
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 3:09 pm

    when you call a function you have to call it by EXACTLY what it is named. Our function is named imageResize so when we call it it has to be exact.

    In other words you can change the name of the function and give it a small r or you put a capital R where you call the function, it doesnt matter just as long as the function name and call are exactly the same.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 3:22 pm

    i checked them all and they're all typed correctly so it cant be that, its got to be something else.
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 3:33 pm

    hmm, thats odd. try changing the name of the function to something a little more simple like "resize" (all lowercase without the quotes). Once you've changed the name of the function in session.php dont forget to change your function call to exactly what you renamed the function, and dont forget to check the case to make sure it matches.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 3:38 pm

    still get the same result Fatal error: Call to undefined function resize() in /home/vol11/xtreemhost.com/xth_4280991/htdocs/userinfo.php on line 49
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 3:41 pm

    ok then im stumped.

    mind posting your session.php and userinfo.php for me please?


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Wed Oct 14, 2009 3:45 pm

    userinfo.php
    Code:

    <?
    /**
     * UserInfo.php
     *
     * This page is for users to view their account information
     * with a link added for them to edit the information.
     *
     * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
     * Last Updated: August 26, 2004
     */
    include("include/session.php");
    ?>

    <html>
    <title>Jpmaster77's Login Script</title>
    <body>

    <?
    /* Requested Username error checking */
    $req_user = trim($_GET['user']);
    if(!$req_user || strlen($req_user) == 0 ||
      !eregi("^([0-9a-z])+$", $req_user) ||
      !$database->usernameTaken($req_user)){
      die("Username not registered");
    }

    /* Logged in user viewing own account */
    if(strcmp($session->username,$req_user) == 0){
      echo "<h1>My Account</h1>";
    }
    /* Visitor not viewing own account */
    else{
      echo "<h1>User Info</h1>";
    }

    /* Display requested user information */
    $req_user_info = $database->getUserInfo($req_user);

    /* Username */
    echo "<b>Username: ".$req_user_info['username']."</b><br>";

    /* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
          if(strcmp($database->username,$req_user_info['avatar']) == 0){
    $mysock = getimagesize("include/userimg/no_avatar.gif");
    echo '<img src="include/userimg/no_avatar.gif" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }
    else{
    $mysock = getimagesize("include/userimg/" . $req_user_info['avatar']);
    echo '<img src="include/userimg/'.$req_user_info['avatar'].'" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }

    /* Firstname */
    echo "<b>Firstname:</b> ".$req_user_info['firstname']."<br>";

    /* Lastname */
    echo "<b>Lastname:</b> ".$req_user_info['lastname']."<br>";

    /* Country */
    foreach (countryArray() as $key => $value) if($req_user_info['country'] == $key) $country = $value;
    echo "<b>Country:</b> ".$country."<br>";

    /* Email */
    echo "<b>Email:</b> ".$req_user_info['email']."<br>";

    /**
     * Note: when you add your own fields to the users table
     * to hold more information, like homepage, location, etc.
     * they can be easily accessed by the user info array.
     *
     * $session->user_info['location']; (for logged in users)
     *
     * ..and for this page,
     *
     * $req_user_info['location']; (for any user)
     */

    /* If logged in user viewing own account, give link to edit */
    if(strcmp($session->username,$req_user) == 0){
      echo "<br><a href=\"useredit.php\">Edit Account Information</a><br>";
    }

    /* Link back to main */
    echo "<br>Back To [<a href=\"index.php\">Main</a>]<br>";

    ?>

    </body>
    </html>

    session.php
    Code:

    <?
    /**
     * Session.php
     *
     * The Session class is meant to simplify the task of keeping
     * track of logged in users and also guests.
     *
     * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
     * Last Updated: August 19, 2004
     */
    include("database.php");
    include_once('function.php');
    include("mailer.php");
    include("form.php");


    class Session
    {
      var $username;    //Username given on sign-up
      var $userid;      //Random value generated on current login
      var $userlevel;    //The level to which the user pertains
      var $time;        //Time user was last active (page loaded)
      var $logged_in;    //True if user is logged in, false otherwise
      var $userinfo = array();  //The array holding all user info
      var $url;          //The page url current being viewed
      var $referrer;    //Last recorded site page viewed
      /**
        * Note: referrer should really only be considered the actual
        * page referrer in process.php, any other time it may be
        * inaccurate.
        */

      /* Class constructor */
      function Session(){
          $this->time = time();
          $this->startSession();
      }

      /**
        * startSession - Performs all the actions necessary to
        * initialize this session object. Tries to determine if the
        * the user has logged in already, and sets the variables
        * accordingly. Also takes advantage of this page load to
        * update the active visitors tables.
        */
      function startSession(){
          global $database;  //The database connection
          session_start();  //Tell PHP to start the session

          /* Determine if user is logged in */
          $this->logged_in = $this->checkLogin();

          /**
          * Set guest value to users not logged in, and update
          * active guests table accordingly.
          */
          if(!$this->logged_in){
            $this->username = $_SESSION['username'] = GUEST_NAME;
            $this->userlevel = GUEST_LEVEL;
            $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
          }
          /* Update users last active timestamp */
          else{
            $database->addActiveUser($this->username, $this->time);
          }
         
          /* Remove inactive visitors from database */
          $database->removeInactiveUsers();
          $database->removeInactiveGuests();
         
          /* Set referrer page */
          if(isset($_SESSION['url'])){
            $this->referrer = $_SESSION['url'];
          }else{
            $this->referrer = "/";
          }

          /* Set current url */
          $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
      }

      /**
        * checkLogin - Checks if the user has already previously
        * logged in, and a session with the user has already been
        * established. Also checks to see if user has been remembered.
        * If so, the database is queried to make sure of the user's
        * authenticity. Returns true if the user has logged in.
        */
      function checkLogin(){
          global $database;  //The database connection
          /* Check if user has been remembered */
          if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
            $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
            $this->userid  = $_SESSION['userid']  = $_COOKIE['cookid'];
          }

          /* Username and userid have been set and not guest */
          if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
            $_SESSION['username'] != GUEST_NAME){
            /* Confirm that username and userid are valid */
            if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
                /* Variables are incorrect, user not logged in */
                unset($_SESSION['username']);
                unset($_SESSION['userid']);
                return false;
            }

            /* User is logged in, set class variables */
            $this->userinfo  = $database->getUserInfo($_SESSION['username']);
            $this->username  = $this->userinfo['username'];
            $this->userid    = $this->userinfo['userid'];
            $this->userlevel = $this->userinfo['userlevel'];
            return true;
          }
          /* User not logged in */
          else{
            return false;
          }
      }

      /**
        * login - The user has submitted his username and password
        * through the login form, this function checks the authenticity
        * of that information in the database and creates the session.
        * Effectively logging in the user if all goes well.
        */
      function login($subuser, $subpass, $subremember){
          global $database, $form;  //The database and form object

          /* Username error checking */
          $field = "user";  //Use field name for username
          if(!$subuser || strlen($subuser = trim($subuser)) == 0){
            $form->setError($field, "* Username not entered");
          }
          else{
            /* Check if username is not alphanumeric */
            if(!eregi("^([0-9a-z])*$", $subuser)){
                $form->setError($field, "* Username not alphanumeric");
            }
          }

          /* Password error checking */
          $field = "pass";  //Use field name for password
          if(!$subpass){
            $form->setError($field, "* Password not entered");
          }
         
          /* Return if form errors exist */
          if($form->num_errors > 0){
            return false;
          }

          /* Checks that username is in database and password is correct */
          $subuser = stripslashes($subuser);
          $result = $database->confirmUserPass($subuser, md5($subpass));

          /* Check error codes */
          if($result == 1){
            $field = "user";
            $form->setError($field, "* Username not found");
          }
          else if($result == 2){
            $field = "pass";
            $form->setError($field, "* Invalid password");
          }
         
          /* Return if form errors exist */
          if($form->num_errors > 0){
            return false;
          }

          /* Username and password correct, register session variables */
          $this->userinfo  = $database->getUserInfo($subuser);
          $this->username  = $_SESSION['username'] = $this->userinfo['username'];
          $this->userid    = $_SESSION['userid']  = $this->generateRandID();
          $this->userlevel = $this->userinfo['userlevel'];
         
          /* Insert userid into database and update active users table */
          $database->updateUserField($this->username, "userid", $this->userid);
          $database->addActiveUser($this->username, $this->time);
          $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);

          /**
          * This is the cool part: the user has requested that we remember that
          * he's logged in, so we set two cookies. One to hold his username,
          * and one to hold his random value userid. It expires by the time
          * specified in constants.php. Now, next time he comes to our site, we will
          * log him in automatically, but only if he didn't log out before he left.
          */
          if($subremember){
            setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
            setcookie("cookid",  $this->userid,  time()+COOKIE_EXPIRE, COOKIE_PATH);
          }

          /* Login completed successfully */
          return true;
      }

      /**
        * logout - Gets called when the user wants to be logged out of the
        * website. It deletes any cookies that were stored on the users
        * computer as a result of him wanting to be remembered, and also
        * unsets session variables and demotes his user level to guest.
        */
      function logout(){
          global $database;  //The database connection
          /**
          * Delete cookies - the time must be in the past,
          * so just negate what you added when creating the
          * cookie.
          */
          if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
            setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
            setcookie("cookid",  "", time()-COOKIE_EXPIRE, COOKIE_PATH);
          }

          /* Unset PHP session variables */
          unset($_SESSION['username']);
          unset($_SESSION['userid']);

          /* Reflect fact that user has logged out */
          $this->logged_in = false;
         
          /**
          * Remove from active users table and add to
          * active guests tables.
          */
          $database->removeActiveUser($this->username);
          $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
         
          /* Set user level to guest */
          $this->username  = GUEST_NAME;
          $this->userlevel = GUEST_LEVEL;
      }

      /**
        * register - Gets called when the user has just submitted the
        * registration form. Determines if there were any errors with
        * the entry fields, if so, it records the errors and returns
        * 1. If no errors were found, it registers the new user and
        * returns 0. Returns 2 if registration failed.
        */
      function register($subavatar, $subuser, $subpass, $subconfpass, $subemail, $subfirstname, $sublastname, $subcountry, $accept_terms){
          global $database, $form, $mailer;  //The database, form and mailer object
         
          /* Username error checking */
          $field = "user";  //Use field name for username
          if(!$subuser || strlen($subuser = trim($subuser)) == 0){
            $form->setError($field, "* Username not entered");
          }
          else{
            /* Spruce up username, check length */
            $subuser = stripslashes($subuser);
            if(strlen($subuser) < 5){
                $form->setError($field, "* Username below 5 characters");
            }
            else if(strlen($subuser) > 30){
                $form->setError($field, "* Username above 30 characters");
            }
            /* Check if username is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", $subuser)){
                $form->setError($field, "* Username not alphanumeric");
            }
            /* Check if username is reserved */
            else if(strcasecmp($subuser, GUEST_NAME) == 0){
                $form->setError($field, "* Username reserved word");
            }
            /* Check if username is already in use */
            else if($database->usernameTaken($subuser)){
                $form->setError($field, "* Username already in use");
            }
            /* Check if username is banned */
            else if($database->usernameBanned($subuser)){
                $form->setError($field, "* Username banned");
            }
          }

          /* Password error checking */
          $field = "pass";  //Use field name for password
          if(!$subpass){
            $form->setError($field, "* Password not entered");
          }
          else{
            /* Spruce up password and check length*/
            $subpass = stripslashes($subpass);
            if(strlen($subpass) < 4){
                $form->setError($field, "* Password too short");
            }
            /* Check if password is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
                $form->setError($field, "* Password not alphanumeric");
            }
            /**
              * Note: I trimmed the password only after I checked the length
              * because if you fill the password field up with spaces
              * it looks like a lot more characters than 4, so it looks
              * kind of stupid to report "password too short".
              */
          }

          /* Email error checking */
          $field = "email";  //Use field name for email
          if(!$subemail || strlen($subemail = trim($subemail)) == 0){
            $form->setError($field, "* Email not entered");
          }
          else{
            /* Check if valid email address */
            $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                    ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                    ."\.([a-z]{2,}){1}$";
            if(!eregi($regex,$subemail)){
                $form->setError($field, "* Email invalid");
            }
            $subemail = stripslashes($subemail);
          }

      /*Firstname error checking */
    $field = "firstname";
    if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0) $form->setError($field, "* Firstname not entered");
    $subfirstname = stripslashes($subfirstname);

    /*Lastname error checking */
    $field = "lastname";
    if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0) $form->setError($field, "* Lastname not entered");
    $sublastname = stripslashes($sublastname);

    /*Country error checking*/
    $field = "country";     
    if($subcountry == '') $form->setError($field, "* Country Not Entered");

    /*Comfirm Password checking*/
    $field = "confpass";
    if(!$subconfpass){
        $form->setError($field, "* Please confirm you password");
    }
    else{
        if(strcmp($subconfpass, $subpass) != 0){
            $form->setError($field, "* Your passwords don't match");
        }
    }

    /* Accept Terms error checking */
    $field = "accept_terms"; 
    if($accept_terms != 1){
      $form->setError($field, "* You must accept the terms to register");
    }

          /* Errors exist, have user correct them */
          if($form->num_errors > 0){
            return 1;  //Errors with form
          }
          /* No errors, add the new account to the */
          else{
            if($database->addNewUser($subavatar, $subuser, md5($subpass), $subemail, $subfirstname, $sublastname, $subcountry)){
                if(EMAIL_WELCOME){
                  $mailer->sendWelcome($subuser,$subemail,$subpass);
                }
                return 0;  //New user added succesfully
            }else{
                return 2;  //Registration attempt failed
            }
          }
      }

      /**
        * editAccount - Attempts to edit the user's account information
        * including the password, which it first makes sure is correct
        * if entered, if so and the new password is in the right
        * format, the change is made. All other fields are changed
        * automatically.
        */
      function editAccount($subcurpass, $subnewpass, $subemail, $subfirstname, $sublastname, $subcountry, $subavatar){
          global $database, $form;  //The database and form object
          /* New password entered */
          if($subnewpass){
            /* Current Password error checking */
            $field = "curpass";  //Use field name for current password
            if(!$subcurpass){
                $form->setError($field, "* Current Password not entered");
            }
            else{
                /* Check if password too short or is not alphanumeric */
                $subcurpass = stripslashes($subcurpass);
                if(strlen($subcurpass) < 4 ||
                  !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
                  $form->setError($field, "* Current Password incorrect");
                }
                /* Password entered is incorrect */
                if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
                  $form->setError($field, "* Current Password incorrect");
                }
            }
           
            /* New Password error checking */
            $field = "newpass";  //Use field name for new password
            /* Spruce up password and check length*/
            $subpass = stripslashes($subnewpass);
            if(strlen($subnewpass) < 4){
                $form->setError($field, "* New Password too short");
            }
            /* Check if password is not alphanumeric */
            else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
                $form->setError($field, "* New Password not alphanumeric");
            }
          }
          /* Change password attempted */
          else if($subcurpass){
            /* New Password error reporting */
            $field = "newpass";  //Use field name for new password
            $form->setError($field, "* New Password not entered");
          }
         
          /* Email error checking */
          $field = "email";  //Use field name for email
          if($subemail && strlen($subemail = trim($subemail)) > 0){
            /* Check if valid email address */
            $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                    ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                    ."\.([a-z]{2,}){1}$";
            if(!eregi($regex,$subemail)){
                $form->setError($field, "* Email invalid");
            }
            $subemail = stripslashes($subemail);
          }
         
                /*Firstname error checking */
          $field = "firstname";
          if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0) $form->setError($field, "* Firstname not entered");
          $subfirstname = stripslashes($subfirstname);

                /*Lastname error checking */
     $field = "lastname";     
    if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0){       
      $form->setError($field, "* Lastname not entered");     
    }         
      $sublastname = stripslashes($sublastname);     

    /*Country error checking */
    $field = "country"; 
    if($subcountry == '') $form->setError($field, "* country not entered");

    $field = "avatar"; //Use field name for avatar
    //Check that we have a file
    if((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {
      //Check if the file is JPEG/GIF and it's size is less than 15Kb
      $filename = basename($_FILES['avatar']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);
      if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "gif") && ($_FILES["avatar"]["type"] == "image/jpeg") || ($_FILES["avatar"]["type"] == "image/pjpeg") || ($_FILES["avatar"]["type"] == "image/gif") &&
      //the size below this line states it must be smaller than 15Kb
        ($_FILES["avatar"]["size"] < 15000)) {
      //the line below this gives the uploaded file a random number name
    $random_digit=rand(0000,9999);

    //combine random digit to you file name to create new file name
    //use dot (.) to combile these two variables
    $newplus=$random_digit.".";
    $subavatar=$newplus.$ext;
            $subavatar = stripslashes($subavatar);

        //Determine the path to which we want to save this file
          $newname = dirname(__FILE__).'/userimg/'.$subavatar;
          //Check if the file with the same name is already exists on the server
          if (!file_exists($newname)) {
            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['avatar']['tmp_name'],$newname))) {
            $database->updateUserField($this->username,"avatar",$subavatar);
            } else {
              $form->setError($field, "* Error: A problem occurred during file upload!");
            }
          } else {
              $form->setError($field, "* Error: File ".$_FILES["avatar"]["name"]." already exists");
          }
      } else {
              $form->setError($field, "* Error: Only .jpg or .gif images please.");
      }
    } else {
             
    }



    /* img resize */
    function resize($width, $height, $target) {

    /* takes the larger size of the width and height and applies the
    formula accordingly...this is so this script will work
    dynamically with any size image */

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    /* gets the new value and applies the percentage, then rounds the value */
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    /* returns the new sizes in html image tag format...this is so you
    can plug this function inside an image tag and just get the */

    return "width=\"$width\" height=\"$height\"";

    }


    /* Errors exist, have user correct them */
          if($form->num_errors > 0){
            return false;  //Errors with form
          }

          /* Update password since there were no errors */
          if($subcurpass && $subnewpass){
            $database->updateUserField($this->username,"password",md5($subnewpass));
          }
         
          /* Change Email */
          if($subemail){
            $database->updateUserField($this->username,"email",$subemail);
          }
         
          /* Change Firstname*/
        if($subfirstname){
        $database->updateUserField($this->username,"firstname",$subfirstname);
        }

        /* Change Lastname */
    if($sublastname){
    $database->updateUserField($this->username,"lastname",$sublastname);
    }

    /* Change Country */
    if($subcountry)
    $database->updateUserField($this->username,"country",$subcountry);



          /* Change Avatar (If the user is uploading a new avatar unlink will delete their old one from the directory "userimg".)*/
          if($subavatar){
    $avadel='include/userimg/'.$this->userinfo['avatar'];
          if (empty($this->userinfo['avatar'])) {$avadel='No Avatar to delete';}
            else
          {
    unlink($avadel);
          }
            $database->updateUserField($this->username,"avatar",$subavatar);
          }

          /* Success! */
          return true;
      }
     
      /**
        * isAdmin - Returns true if currently logged in user is
        * an administrator, false otherwise.
        */
      function isAdmin(){
          return ($this->userlevel == ADMIN_LEVEL ||
                  $this->username  == ADMIN_NAME);
      }
     
      /**
        * generateRandID - Generates a string made up of randomized
        * letters (lower and upper case) and digits and returns
        * the md5 hash of it to be used as a userid.
        */
      function generateRandID(){
          return md5($this->generateRandStr(16));
      }
     
      /**
        * generateRandStr - Generates a string made up of randomized
        * letters (lower and upper case) and digits, the length
        * is a specified parameter.
        */
      function generateRandStr($length){
          $randstr = "";
          for($i=0; $i<$length; $i++){
            $randnum = mt_rand(0,61);
            if($randnum < 10){
                $randstr .= chr($randnum+48);
            }else if($randnum < 36){
                $randstr .= chr($randnum+55);
            }else{
                $randstr .= chr($randnum+61);
            }
          }
          return $randstr;
      }
    };


    /**
     * Initialize session object - This must be initialized before
     * the form object because the form uses session variables,
     * which cannot be accessed unless the session has started.
     */
    $session = new Session;

    /* Initialize form object */
    $form = new Form;

    ?>
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Wed Oct 14, 2009 6:24 pm

    alright so ive been at it just over an hour now and i cant re-create the error you've described with the code you've given me. but i do see you are including some kind of functions script inside of session.php. maybe try removing the sitepoint function from session.php and putting it inside your function script.

    let me know what that does.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mattastic

    Number of posts : 25
    Registration date : 2009-10-11

    Re: avatar system

    Post  mattastic on Thu Oct 15, 2009 7:24 am

    HUGE thanks that did the trick. i finally got it working. i actually tried that before but i forgot to try it again after i fixed the comments in it. now how do you make the image appear on another page? i tried using the code you put in your userinfo.php

    Code:

    /* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
          if(strcmp($database->username,$req_user_info['avatar']) == 0){
    $mysock = getimagesize("include/userimg/no_avatar.gif");
    echo '<img src="include/userimg/no_avatar.gif" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }
    else{
    $mysock = getimagesize("include/userimg/" . $req_user_info['avatar']);
    echo '<img src="include/userimg/'.$req_user_info['avatar'].'" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }

    but it only calls the no_avatar.gif.
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: avatar system

    Post  Linchpin311 on Thu Oct 15, 2009 8:33 am

    well on any other page you probably arent running a query to make $req_user_info a valid array so i would mimic the way userinfo.php does it. first you would have to pass the username to the URL so we can call it in a function later. then, on whatever page you want to display the image, somewhere near the top, try putting:
    Code:
    /* Requested Username error checking */
    $req_user = trim($_GET['user']);
    if(!$req_user || strlen($req_user) == 0 ||
      !eregi("^([0-9a-z])+$", $req_user) ||
      !$database->usernameTaken($req_user)){
      die("Username not registered");
    }

    /* Display requested user information */
    $req_user_info = $database->getUserInfo($req_user);

    that will create the $req_user_info array and then you can just put the same code as used in userinfo.php to display an avatar:
    Code:
    /* Avatar (assumes you have a default for folks who don't upload an avatar, here it is called "no_avatar.gif")*/
    if(strcmp($database->username,$req_user_info['avatar']) == 0){
      $mysock = getimagesize("include/userimg/no_avatar.gif");
      echo '<img src="include/userimg/no_avatar.gif" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }
    else{
      $mysock = getimagesize("include/userimg/" . $req_user_info['avatar']);
      echo '<img src="include/userimg/'.$req_user_info['avatar'].'" ' . resize($mysock[0], $mysock[1], 150) . '/>';
    }


    Now if you were thinking more like a member list page (like the one in this forum) where all usernames are displayed with their avatars beside them that may be a little trickier. Just off the top of my head i can see you needing to count the number of usernames in your database and using that as the limit in a for loop which more or less has the same code we just used before to display your image...


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.

    Sponsored content

    Re: avatar system

    Post  Sponsored content


      Current date/time is Tue Sep 19, 2017 9:29 am