num_rows() problem |
[eluser]eldrinofsoalim[/eluser]
Hi guys, I have a problem with this code: Quote:if ($this->session->userdata('username') == "" ) { echo "Blank"; } else { echo "Not Blank"; } It produces this output: Quote:Blank<br /> My 'username' cookie is empty or blank (as shown in first line of code). HOWEVER, whenever I use the Active Records get_where() function, where table name is 'users' and where field name 'username' is equal to the username cookie (remember, it's blank), it always returns 1. Why does it return 1 when there is no blank username in the table? Hoping for your help. eldrinofsoalim
[eluser]InsiteFX[/eluser]
Code: if ($this->session->userdata('username') == "") InsiteFX
[eluser]eldrinofsoalim[/eluser]
Hi InsiteFX, I tried your code and it still produces the same result. $query->num_rows(); still produces 1 even though the username cookie is blank there is no blank username in the database.
[eluser]InsiteFX[/eluser]
Did you load the session library? Code: $this->load->library('session'); InsiteFX
[eluser]eldrinofsoalim[/eluser]
Yes, I did. It's autoloaded in config. Quote:$autoload['libraries'] = array('database','session'); I'm using this to check if the username cookie set, and if set, will redirect the user to members section, or if not, redirect him/her to the sign-in page. Are you also producing the same result?
[eluser]InsiteFX[/eluser]
Is the session userdata being set with the users username? InsiteFX
[eluser]eldrinofsoalim[/eluser]
Not simultaneously. The userdata is set when the controller calls the validate() function in the sign-in controller, which calls $this->session->set_userdata($newuserdata). For my code in the initial post, I'm just trying to check if the user is logged in via the session cookie. If logged-in, redirect to members-only controller. If not, redirect to sign-in controller.
[eluser]InsiteFX[/eluser]
When the user login set this in your login method: Code: $newdata = array( Check to see if user is logged_in in your controller: Code: if (logged_in() == TRUE) Add this to auth library if you have one. Code: public function logged_in() You may need the CI Instance for the above! InsiteFX
[eluser]eldrinofsoalim[/eluser]
Great! That did the trick! Thanks InsiteFX. Is that the proper way to check if the user is logged-in during a website? Although, I'm still wondering why it still returned a num_row() when obviously there wasn't any blank username.
[eluser]InsiteFX[/eluser]
It returned 1 for the row because it did find the row in the database. InsiteFX |
Welcome Guest, Not a member yet? Register Sign In |