Demo Blog Tutorial

Ok, I been following the blog tutorial and every thing seems to be ok. I just got to the one page with I setup the users login.

This is the page I’m stuck on.

http://www.yiiframework.com/doc/blog/1.1/en/prototype.auth

I keep getting a on the code that looks in the database for the users info. Look at this code below




<?php

class UserIdentity extends CUserIdentity

{

    private $_id;

 

    public function authenticate()

    {

        $username=strtolower($this->username);

        $user=User::model()->find('LOWER(username)=?',array($username));

        if($user===null)

            $this->errorCode=self::ERROR_USERNAME_INVALID;

        else if(!$user->validatePassword($this->password))

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

        else

        {

            $this->_id=$user->id;

            $this->username=$user->username;

            $this->errorCode=self::ERROR_NONE;

        }

        return $this->errorCode==self::ERROR_NONE;

    }

 

    public function getId()

    {

        return $this->_id;

    }

}



The problem I’m having is that I get a Parse error: parse error in C:\www\vhosts\localhost\_\basicsite\protected\components\UserIdentity.php on line 28

and the line below is where the error is. I guess it doesn’t exspect the else statement.

    else if(&#33;&#036;user-&gt;validatePassword(&#036;this-&gt;password))

Here’s all of my code that I had to change. can some one give me an idea on how to fix this.




<?php


/**

 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.

 */

class UserIdentity extends CUserIdentity

{

	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the username and password

	 * are both 'demo'.

	 * In practical applications, this should be changed to authenticate

	 * against some persistent user identity storage (e.g. database).

	 * @return boolean whether authentication succeeds.

	 */

	

	private $_id;

	

	public function authenticate()

	{	

		$username = strtolower($this->username);

		$user=User::model()->find('LOWER(usename)=?',array($username));

			if($user == null)

			{

					$this->errorCode = self::ERROR_USERNAME_INVALID;

				else if (!$user->validatePassword($this->password))				

					$this->errorCode = self::ERROR_PASSWORD_INVALID;

			

			else

			{

				

				$this->_id = $user->id;

				$this->username = $user->username;

				$this->errorCode=self::ERROR_NONE;				

			}

			

			return $this->errorCode=self::ERROR_NONE;

			}

		

		

		/*

		$users=array(

			// username => password

			'demo'=>'demo',

			'admin'=>'admin',

		);

		if(!isset($users[$this->username]))

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if($users[$this->username]!==$this->password)

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

			$this->errorCode=self::ERROR_NONE;

		return !$this->errorCode;

		*/

	}

	

	public function getId()

	{

		return $this->_id;

	}

}



Thanks

Joe

I fount the problem and fixed it. I was using a { at the top of the if statement and didn’t need it. Now the problem is that it can’t find the




 public function hashPassword($password,$salt)

    {

        return md5($salt.$password);

    }



The problem here is that salt is not defined, So how would I use the method below to fix this.




/**

 * Generates a salt that can be used to generate a password hash.

 * @return string the salt

 */

protected function generateSalt()

{

    return uniqid('',true);

}



So I’m confused why the $salt was never defined if it is needed.

Hi,

I think the generateSalt() method is required in User class, not in UserIdentity class.

And the latest sample code bundled with yii has already defined it in User.php.

Well I think you have also a problem in these two lines in your authenticate function


    public function authenticate()

    {

        …

        if($user == null)

        { // this line is not needed, you've figured it out

                $this->errorCode = self::ERROR_USERNAME_INVALID;

        …

        return $this->errorCode=self::ERROR_NONE; // you're basically resetting errorCode no matter what was its value

        } // this line is not needed either

        /*

        …

        */

    }

It looks that you’ll never be able to log in. Look closely, it’s different from your reference code


    public function authenticate()

    {

        …

        return $this->errorCode==self::ERROR_NONE; // here it's returning a comparison

    }

Thanks. I check. This is where this is defined at. The tutorial says User.php and I guess the $salt is missing. any ideas on how to add this.

Thanks for the info. and I’m sorry. This is the 1st time for me to read threw the blog tutorial. If you find an error in the tutorial. Then you should report it. I just check and this is how the tutorial reads. The problem I need to figure out is why the User Model doesn’t see the $salt. I need to know how to define it so I can use it.

I don’t see an download link to download the demo’s they have on line. Or I would have just use the code from those sources. If you know where I can download the blog demo. Please send me a link.

Because User model is a CActiveRecord, it automatically has an attribute named ‘salt’ that represents the ‘salt’ column of a row in the user table. It can be accessed via $this->salt in User model methods. You don’t have to(or, you should not) add it manually.

Hmm I may have not been clear enough. The blog tutorial and especially the code snippet you quoted, are correct. You haven’t typed the code exactly like it is shown in the tutorial. Check again my post and see the difference (hint: you typed [color="#FF0000"][font=“Courier New”]=[/font][/color] and the tutorial has [font=“Courier New”][color="#2E8B57"]==[/color][/font]).

It also seems you’re looking for the demos. You have them when you download the framework. Look into <folder where you extracted the framework archive>/demos. It can’t get any simpler :wink: You’ll find 4 demos:

  • blog

  • hangman

  • phonebook

  • helloworld

It’s included in the download package of yii.

  • yii-1.1.10.r3566

[list]

  • demos

[list]

  • blog … here

  • hangman

  • helloworld

  • phonebook

[*]framework

[*]requirements

[/list]

[/list]

[EDIT]

… as bennouna has already posted.

Thanks Guys. After going threw the demo blog and reading threw the tutorial. I fount out that the code in the tutorial and the code in the blog demo don’t match up. What I mean is there’s code that’s in the tutorial that’s not in the blog demo. and Well I been recoding every thing. From the models to the page views. and I thought that GII was to do this for me. and I guess not. There’s functions missing in the blog demo that’s list and in the tutorial for the blog. So it has me so confused.

I think that the coders of YII have a lot more work then what I thought when I started using this framework. The doc’s say that using GII will cut down on the amount of time editing pages and so on. Well I just spend the last 3 hours recoding pages and adding in functions that were missing just to find out that more functions are still missing from the blog demo that is on the tutorial.

I have done OOP before, nothing like the way this YII is coded. I would have to learn some thing all too new. and right now. If I don’t get this web site up and online soon. I be scraping my Visual C# Software too. Sense it needs this web site for the online options. I’ll have to wait until some one comes up with a video tutorials that will show me from start to end. and then some. I just don’t all ways under stand what I read.

Guys having only one eye sucks.

Try Larry Ullman’s tutorial:

I second Bianca.

Forget about the video tutorials. You’ll only waste your time waiting for good ones to appear. Because making of video tutorials must be very expensive(time consuming), and even with the best effort, they should fall far behind the written ones in terms of their coverage and accuracy. And the utmost drawback of video tutorials is, in my opinion, it will cost multiples of time to keep them up-to-date. They tend to become outdated. In short, making videos doesn’t pay at all. It’s true not only with yii, but also with all the free software in the wild, I think.

If I were you, I would hire a personal tutor who can teach me via skype or remote desktop … only if I could afford it. Otherwise, I would teach myself reading those fxxking dxxned tasteless documents, as there are no other means. ;)

By now, yii is quite a useful tool for making a web app, and Gii is indeed a magic wand for me. But it required me a background knowledge of PHP, SQL, HTML and CSS … not too much of them, but only the basics … and some weeks of painful and uneasy time learning yii. 3 hours? You just didn’t do anything yet.

Thanks. I agree with you on some of it. and creating videos doesn’t cost a think. Other then time. There’s all kinds of desktop recording software you can get for free and easy to use. and then you would use Windows Movie maker to reduce the size of your video and then upload it to you tube.

Any way I been playing a round with YII and I like it. It’s better then any framework I have played with so far for the last 3 weeks. It’s just that it still requires a lot of editing.

It’s just that, well I tried to install a User Manager into the project I have now. and well that didn’t work. I I keep getting an error when I try and install. I’m sorry I don’t remember what the link is.

Do you know of a User Manager for Yii that works. any way back to what you and I were chatting about.

See Video tutorials I think are a lot better then the written ones. Why because they explain to me just how to do some thing and then they show me right on their desktop. and videos cam all ways be updated. when needed.

See the project I started in Yii is or will be a very large project. Look. This is what I wanted to add to this project so I can replace the old project.

A Game mission editing download section for sample missions

A Blog

A Articles section.

A HelpDesk

A Forums Section (Small)

A Portal on the main index page.

A PM System

A Users CP

A Admins CP

A FAQ Section

and more to come

I wanted to add what I could. Like I said there's still a lot of editing in Yii that I would have to do in order to get what I wanted. See the reason for me even starting on a frame work any way is the ACL System. How ever I haven't been able to get support for the ACL on Yii any way. The problem is that Yii lacks support.

If I had the money I would just hire some one to make the web site I wanted. Like you all ready know. They all want money and they are not cheap. So I guess until I can find some one who has nothing else to do and knows how to use Yii. I’m stuck using my old project.

Thanks

Joe

Wow, it’s a very big project!

I wouldn’t try to renew it all at once … it could be very risky. I would stick to the old project as long as I can. :P

As for user management, I’m using these extensions:

  • yii-user

  • rights

yii-user is for authentication and rights for authorization.

Both of them have good reputation in the community.

You may find some difficulties using them together. This forum topic could be helpful.

yii-user with rights

Unfortunately, this topic started way back in November 2010. Some posts have been outdated by the updated versions of yii-user and rights. But somehow it was helpful to me.

Someone else may point you a better link.

Thanks. I’ll give them a try

Thanks again how ever I tried to setup the yii_user and I added the ‘user’, to the modules in the main.php config and well it’s telling me that the source file is missing. So I have no idea what to do. Here’s the link for it. http://filebox.dtdns.net/basicsite/index.php/post/index you can check it out, maybe you can tell me why this doesn’t work.

From what I can tell. This yii-user refers to a user module of some kind. So I did a search on google and fount a module called User_Management_Module_0.5.zip. and I set it up like the doc’s tell me too. and then I run the installer and every think went fine. Until I tried to log in and now all my pages won’t load in. If you visit the link I gave you above you can see the errors. So now I’m so lost on what to do.

Just a guess but I think that module uses ‘tbl_’ prefix in database.

Try this in your main config file:


'db'=>array(

  // .....

  'tablePrefix' => 'tbl_', //<---

),

Yes, That’s set up right. I do have the ‘tablePrefix’ => ‘tbl_’, in my main.php file.

UPDATE:

I checked the tables in the database and they were all names wrong when I installed it. So I changed them to all have the tbl_ prefix and well I still have problems. It says some thing about a ‘t’. just load that link I sent you and you can see.