Page 1 of 1
can anyone explain this code?
Posted: Fri Aug 22, 2008 8:04 am
by zplits
Code: Select all
$count=mysql_num_rows($result);
if ($count==1) {
$data = mysql_fetch_assoc($result);
$_SESSION['user'] = array('id' => $data['id'], 'realName'=>$data['firstName'] . ' ' . $data['lastName']);
header("location:main.php");
exit();
}
Is there anyone who knows what this means?
Thanks in advance
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:06 am
by Stryks
I should. I think I helped you with that code.
Which part don't you get? Or you just don't get any of it?
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:17 am
by zplits
hello sir. Sure you did. It's been a how many days since you helped me. Thank you very much.
This part sir,
Code: Select all
$data = mysql_fetch_assoc($result);
$_SESSION['user'] = array('id' => $data['id'], 'realName'=>$data['firstName'] . ' ' . $data['lastName']);
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:28 am
by Stryks
Okay, Well ... At that point in the code, you have called mysql_query(). You call mysql_num_rows() to find out how many rows of data were returned.
If the number of rows returned == 1 (actually, this should either be > 0 or the SQL should have LIMIT 1 added to the end) then fetch the row of data that was found and hold that data as $data.
Then we set the session key ['user'] to be an array, which holds the users id, and the users realname (being the firstname, a space, and the last name joined together).
After this line, you have $_SESSION['user']['id] and $_SESSION['user']['realname'] available on all subsequent pages that this user views on which you have called session_start(), up until the session is destroyed or times out.
Go to
http://www.php.net and do a search for array() ... it can give you an idea of how arrays work. it probably just seems more complex because we have used it with the $_SESSION variable.
The next two lines are a redirect to another page.
Hope that helps
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:40 am
by zplits
okay sir, I think I'll read the php manual. And by the way, regarding what you said
If the number of rows returned == 1 (actually, this should either be > 0 or the SQL should have LIMIT 1 added to the end)
What should i do in my code? do i need to change it into > 0? what is the difference between >0 and ==1?
Thank you sir.
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:53 am
by Stryks
Well ... it shouldn't really matter too much. You would have to assume that it would be near impossible that your query where username = x and password = y would return more than one result.
But let's just say that by some strange situation, you had two users with the same username and password. Maybe a double submit, or a manual entry or something. mysql_num_rows() would equal 2 (or more), so the code would see that it doesn't equal one, therefore treat it the same as if there were no matches.
But there WERE matches.
So I guess it;s just a logic thing. It's the difference between saying, "do this if exactly one result is returned", and "do this if any result is returned".
And I could go on and on here about arrays for you, but I couldn't really tell you anything any clearer than the manual can.
What I will say is that array's can seem odd at first, but if you take a look at the examples ... replicate a few ... and soon you'll be using them everywhere. They can just be a bit tricky to grasp, that's all.
Have a read and re-read of the manual page and then if you still are baffled, post back and we'll see what we can add.
Cheers

Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:55 am
by Stryks
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 8:59 am
by zplits
Okay sir. Thank you very much...You make it more clearly to me. Yes i agree, arrays are really tricky.
I have another question. But i think it's out of the subject. Because I'm curious on the security of my website. I have set up a protected directory in my host. And the thing that i want to know is how am i be able to edit the php.ini file in my server. Like for example Error_reporting = all....something like that. Do you have any idea regarding it?
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 9:07 am
by Stryks
Generally, most hosts will have error reporting turned down to errors only (no warnings) and even then, a lot of production boxes send the error information to a log file instead of to the screen.
The result is an ominous white screen with no identification of what just happened.
Host's generally wont let you edit the php.ini file directly (too much in there to ruin). There are other ways to override some settings (.httaccess files for example), but for testing purposes, it's probably just as easy to insert ...
Code: Select all
ini_set('display_errors', 1);
error_reporting(E_ALL);
... into the top of pages you are currently testing.
You'll want to remove those lines when the page goes public though. Sometimes those error messages can give away information you don't want malicious users to have, hence the reason they are often hidden.
If it is for testing purposes, you might also want to consider setting up a testing server on your own machine. That way you can set up php.ini however you like.
Cheers
Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 9:13 am
by zplits
WOW, you're really good sir. How do you know those lots of info. Honestly, It's good to know that you're online....Because you really help. And your help are working. Are you one of the developers of php?

It seems like you know a lot in it. Glad to know you.
Sir, is it okay for you if i PM you if i need help? Because I'm sure 100% sure, you'll help. How i wish I'm as knowledgeable as you are. So is it okay with you?
Thank you very much.

Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 12:51 pm
by desmi
I think you should ask your questions/problems here, so others can read it too, and learn that way..

Re: can anyone explain this code?
Posted: Fri Aug 22, 2008 7:36 pm
by Stryks
desmi wrote:I think you should ask your questions/problems here, so others can read it too, and learn that way..

I couldn't agree more. It's a community effort, so asking a question helps everyone. Someone else can learn from your question - learn from the answer - or learn from trying to help you with your problem. But if you post a question and you want to PM me to ask to take a look .... no harm in that.
But seriously, there are people here who are way over my head. Everything I have learned is either from working on projects or on here asking and posting questions. I'm good at certain things, but there are other areas where I have little experience. That's where this place comes in ... there are specialists in this board that can help out with the most complex of problems. Just reading their posts is an education itself. Go read some of the great posts by the PHPDN award winners and see for yourself.
Have a look at my profile page and take a look at all my posts. Then go back to the earliest ones. I was new to PHP and as confused as everyone else when I started. But I still post the occasional question myself.
Stick with it, take the time to grasp fundamentals (like arrays) and learn by doing, and you'll be banging out great code in no time. Then you can use that experience to help out others in here.

Re: can anyone explain this code?
Posted: Sat Aug 23, 2008 6:46 am
by zplits
Thank you very much sir Stryks. I agree with what you said.
But if you post a question and you want to PM me to ask to take a look .... no harm in that.
That's what i mean sir, I'll PM you if i have already posted a question. I'll PM you right away, because i know, you'll help. I knew it.

Thank you very much.
By the way do you remember the DSS system i am talking about? Well, I'll b starting it on Monday; please expect a PM from me to you.
I'm really thankful for all your support, effort and help sir, you're the best. GOD bless you.