parse error i can't fix ---

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

parse error i can't fix ---

Post by glennnphp »

i have an htpassword.php file that works just fine on one godaddy server but i'm getting this parse error on another:

parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/content/j/s/i/jsimkav/html/headers/htpasswd.php on line 33

can someone point out the problem for me?


line 33:

Code: Select all

 
 
class htpasswd{
   public $users;     << 33
   public $error;
   private $_path;   
 
   function htpasswd($file=false){
      if(!$file){
         die('Please specify a file!');
      }else{   
         //configure
         $this->_path = $file;
         $this->users = '';
         //load database
         if(file_exists($file)){
            $data = array();
            $fcontents = file($file);
            while(list($line_num, $line) = each($fcontents)){
               $user = explode(':',$line);
               //$user = $arraydata[0];
               $data[$user[0]] = rtrim($user[1]);
            }
            $this->users = $data;
         }
      }
   }
         
   function create($user, $passwd, $update=false){
      $this->error = '';
      if(isset($this->users[$user]) && !$update){
         $this->error = 'User <strong>'.$user.'</strong> exists! To update the user set the update parameter to true.';
         return false;
      }
      $this->users[$user] = $this->non_salted_sha1($passwd);
      return true;
   }
   
   function remove($user){
      $this->error = '';
      if(isset($this->users[$user])){
         unset($this->users[$user]);
         return true;
      }else{
         $this->error = 'User <strong>'.$user.'</strong> does not exist!';
         return false;
      }
   }
   
   function users(){
      $this->error = '';
      $rval = Array();
      if(is_array($this->users)){
         foreach(array_keys($this->users) as $uid){
            $rval[count($rval)] = $uid;
         }
      }
      return $rval;
   }
      
   function validate($user, $pass){
      $this->error = '';
      if(!isset($this->users[$user])) return False;
      $crypted = $this->users[$user];
      
      if(substr($crypted, 0, 6) == "{SSHA}"){
         $ohash = base64_decode(substr($crypted, 6));
         return substr($ohash, 0, 20) == pack("H*", sha1($pass . substr($ohash, 20)));
      }elseif(substr($crypted, 0, 5) == "{SHA}"){
         return ($this->non_salted_sha1($pass) == $crypted);
      }else{
         return ($pass == $crypted);
      }
   }
 
   function save($file=false){
      $fcontents = "";
      if($file == false) $file = $this->_path;
      foreach(array_keys($this->users) as $user){
         $fcontents .= $user.":".$this->users[$user]."\n";
      }
      if(file_put_contents($file, $fcontents)){
         $this->error = '';
         return true;
      }else{
         $this->error = 'Couln\'t save the file!';
         return false;
      }
   }   
 
   //encryption functions
   function rand_salt_crypt($pass){
      $salt = "";
      mt_srand((double)microtime()*1000000);
      for ($i=0; $i<CRYPT_SALT_LENGTH; $i++)
         $salt .= substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./", mt_rand() & 63, 1);
      return "$apr1$".crypt($pass, $salt);
   }
   
   function rand_salt_sha1($pass){
      mt_srand((double)microtime()*1000000);
      $salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
      return "{SSHA}".base64_encode(pack("H*", sha1($pass . $salt)) . $salt);
   }
   
   function non_salted_sha1($pass){
      return "{SHA}".base64_encode(pack("H*", sha1($pass)));
   }
}
 
//php4 work around
if(!function_exists('file_put_contents')){
   function file_put_contents($filename, $content, $flags = null, $resource_context = null){
      if(is_array($content)){
         $content = implode('', $content);
      }
      if(!is_scalar($content)){
         trigger_error('file_put_contents() The 2nd parameter should be either a string or an array', E_USER_WARNING);
         return false;
      }
      $length = strlen($content);
      $mode = ($flags &FILE_APPEND) ? 'a' : 'w';
      $use_inc_path = ($flags &FILE_USE_INCLUDE_PATH) ? true : false;
      if(($fh = @fopen($filename, $mode, $use_inc_path)) === false){
         trigger_error('file_put_contents() failed to open stream: Permission denied', E_USER_WARNING);
         return false;
      }
      $bytes = 0;
      if(($bytes = @fwrite($fh, $content)) === false){
         $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s',
         $length,
         $filename);
         trigger_error($errormsg, E_USER_WARNING);
         return false;
      }
      @fclose($fh);
      if($bytes != $length){
         $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
         $bytes,
         $length);
         trigger_error($errormsg, E_USER_WARNING);
         return false;
      }
      return $bytes;
   }
}
?> 
 
 
 
 
 
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: parse error i can't fix ---

Post by Cut »

Shouldn't there be a space after htpasswd?

Code: Select all

class htpasswd {
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: parse error i can't fix ---

Post by markusn00b »

Cut wrote:Shouldn't there be a space after htpasswd?

Code: Select all

class htpasswd {
No. :D

I'm guessing you're using PHP < 5 ? In PHP 5, the 'var' keyword is deprecated (expecting T_VAR). But, because you're getting this error (T_VAR), I'm thinking you're using PHP 4, where the 'var' keyword is wanted. So try:

Code: Select all

 
class htpasswd{
   public var $users;     << 33
   public var $error;
   private var $_path;  
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: parse error i can't fix ---

Post by josh »

Also what is this

Code: Select all

 
<< 33   
 
My editor shows errors both on the "var" and on that part
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: parse error i can't fix ---

Post by markusn00b »

jshpro2 wrote:Also what is this

Code: Select all

 
<< 33   
 
My editor shows errors both on the "var" and on that part
Haha, I believe that's him showing us where line 33 is :lol:
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: parse error i can't fix ---

Post by marcth »

[quote="markusn00b"][quote="jshpro2"]Also what is this

Code: Select all

 
<< 33   
 
In PHP 5 you don't need to use the var keyword. Can you try adding a function before your class to see if the error moves? What line is the error on now, after adding this function?

Code: Select all

 
function boo() {
  echo 'Does the error appear here now?';
}
boo();
 
class htpasswd{
  public $users;     // << 33
  public $error;
  private $_path;   
  // ....
}
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: parse error i can't fix ---

Post by josh »

markusn00b wrote:Haha, I believe that's him showing us where line 33 is :lol:
my bad
Post Reply