need help on user login

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

User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Well ... we stored information into $_SESSION['user'] as an array so that we could keep track of it. Problem is, if you try to echo an array, it just says 'array'.

We used ...

Code: Select all

$_SESSION['user'] = array('id' => $data['id'], 'firstName'=>$data['firstName']);
... to store the data into the session ... so we should be able to specify what part of the array we want to echo with something like ...

Code: Select all

echo $_SESSION['user']['firstName'];
You might want to have a read of THIS MANUAL PAGE to get up to speed on how arrays work.

8)
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

thanks for the info sir. Still no luck, no name displayed, went back to display just the logout button
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Oh ... hang on ... we've taken a few steps backwards here.

The last time you posted checklogin.php ... you had this ...

Code: Select all

$_SESSION['user'] = array('id' => $data['id'], 'firstName'=>$data['firstName']);
But as you have not pulled that data from the database ....

Code: Select all

$sql="SELECT id FROM $tbl_name WHERE loginName='$myusername' and passWord='$mypassword'";
... $data['firstName'] is not available to set into the $_SESSION array. Remember, we can only use information from the database that we actually pull. You will know if you have pulled it because it will be named between the SELECT and FROM sections of your SQL query.

Given your DB schema, this ...

Code: Select all

$sql="SELECT id, firstName, lastName FROM $tbl_name WHERE loginName='$myusername' and passWord='$mypassword'";
... would be more appropriate.

So now you will have access to id, firstName and lastName. So seeing as we now have both of the users real names ... we might as well use them both.

Code: Select all

$_SESSION['user'] = array('id' => $data['id'], 'realName'=>$data['firstName'] . ' ' . $data['lastName']);
Now the $_SESSION array key is 'realname', not 'firstName', so we display it with ...

Code: Select all

echo $_SESSION['user']['realname'];
And all things being equal .. that should be all happy. :D
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

hey sir. That's it. I'm so happy we got it at last. Thank you very much for your help. Very much appreciated. How i wish i'm as good as you. I also want to help other people. I'm new in php but I'm kinda good in flash. :) Wanna see some of my works?

Sir, can i ask you something about sessions?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

We're rolling on sessions so why not. What you need to know?

If you want to get better and help people out, just keep working on your project and swing past here every so often. You can learn a whole lot by reading other peoples posts, and every so often, you might have something valuable to add.

Always happy to look .... drop a link if you have them online someplace.

Cheers
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

:) yes sir. You're right. Sad to say I'm kinda busy right now. Because I'm a information technology graduating student, and I'm having my thesis. But I also spend time helping my classmates or schoolmates in their problems. Anyway i can help. I'll be glad to help.

Here is some of my flash works:
http://www.whatsthelatest.net/lfisher
http://www.whatsthelatest.net/kaisei
http://www.whatsthelatest.net/chickendeli

Here is my blog:
http://www.whatsthelatest.net

The site i have created for my church:
http://www.ioacjci.org

Hope you like it....

My question in session is, where should i put it? i mean should i put it in every page?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

Nice work. I've never worked with flash .... have always been more content driven. But if I ever give it a go I might hit you up for some answers. :lol:

As for the sessions, basically, you need to include $session_start() on every page which will access $_SESSIONS in any way. So you'll need to have it on every page that is restricted to logged in users for example, so that you'll have the 'users' data to verify they are logged in. i.e. if it exists then they have logged in.

For the most part, it doesn't hurt to start them on every page anyhow.

Good luck
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Sure sir, I'll be glad to help in exchange. :)

Ahm. Should i insert:

Code: Select all

session_start();
if(!isset($_SESSION['user'])){
header("location:login.php");
}
in every page? or i'll just insert session_start();?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: need help on user login

Post by Stryks »

The whole block will probably be best ...

This will mean that people who are not logged in will be sent back to the index page.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: need help on user login

Post by zplits »

Okay sir. Thank you very much all of your help and support. I really appreciate it. This login system won't be possible without your great and quick responses.

I am now planning to add a Lost your password? in the login system.
I hope you'll still help me. cheers. Your the man sir. Thanks for everything.
Post Reply