ady_bavarezu89 Posted May 27, 2013 Share Posted May 27, 2013 (edited) Hello everyone! I need help with a problem I'm trying to solve for 2 days now. I know there must be a way and I hope you guys can help me out. What I have here is this. In my website the user is logging in, and I check in the mysql database if the user and password matches. If yes I want to save in php variables all my values from the database such as username, firstname, lastname to use it later on. I can do this but the problem is everytime I go to the page where I use those variables, those are reinitialized and I get the error: Notice: Undefined index: user_id I will write a short example of code with what I try to do: //A.php<?phprequire 'core.inc.php'; // core.inc.php contains the connection to the db and the session_start() login_form(); function login_form(){ $username = $_POST['username']; $password = $_POST['password']; // if username and password are set I do a query on the db and check if it matches // if it matches $_SESSION['user_id'] = $user_id; } ?>--------------------------------------------------------------------------// B.php<?php include 'A.php'; echo $_SESSION['user_id']; ?>Now this works perfectly fine first time I go to B.php the output is 1 or 2 or whatever the session is. But if I go to another page and then go to B.php again the include function is called again so A.php isloaded again, A.php will call core.inc.php so the session is restarted and my variables are set to 0which I don't want. I've tried a lot of things, include_once, include only the variables, global variables, check if the session is started. Nothing worked...Do you lads have a suggestion for me? I would really appreciate it! Edited May 27, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/ Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 You should only call that function when the form is actually submitted. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432573 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 (edited) You should only call that function when the form is actually submitted. You mean login_form()? How can I submit my form without calling this function? And would this make a difference since the session will restart anyway if I go to B.php again? Isn't there a posibility to include in B.php only the variables? so I won't have to load the whole page? Edited May 27, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432574 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 ... You should only call the login_form() function when your form has been submitted. Not every single page. When the form is submitted, it will go to the file which is designated in the action attribute of the form element. That page should check to see if the form was submitted by seeing if $_POST['submit'] (or whatever you called the button) isset. If isset returns true, attempt to login. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432579 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 ... You should only call the login_form() function when your form has been submitted. Not every single page. When the form is submitted, it will go to the file which is designated in the action attribute of the form element. That page should check to see if the form was submitted by seeing if $_POST['submit'] (or whatever you called the button) isset. If isset returns true, attempt to login. Okay...so I have my form on index.php <form action="a.php" method="POST"> <!-- Username --> <p class="label label-info">Please enter your username: </p> <input type="text" name="username" placeholder="username" required="required"> <!-- Password --> <p class="label label-info">Please enter your password: </p> <input type="password" name="password" placeholder="password" required="required"> <button class="btn btn-primary" type="submit" name="submit"> login </form> You mean I should do something like this: if { isset($_POST['submit']) A.php.login_form(); } ? And that would mean something like if the submit button was pressed? Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432582 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 I don't know why you put the A.php in front of your function call, but otherwise close. Your if is messed up too. Please use code tags when you post code. <> button. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432584 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 I don't know why you put the A.php in front of your function call, but otherwise close. Your if is messed up too. Please use code tags when you post code. <> button. Sorry my bad...I put the A.php because the login_form method is in another page, not in index.php where I have the form and I mistook it with java ). what I wanted to say is: //index.php include 'A.php'; if ( isset($_POST['submit']) ) { login_form(); } Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432592 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 Well, did it work? Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432603 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 Well, did it work? By including the A.php in Index.php I get this error Notice: A session had already been started - ignoring session_start() Because A.php includes core.inc.php which starts the session. I feel like I'm running in a circle here Even with this error after I attempted to login the page just freezed. I added this code: <button class="btn btn-primary" type="submit" onclick="afunction"> <i class="icon-ok-circle icon-white"> </i> login</button> <script> function afunction(){ if ( isset($_POST['submit']) ) { login_form(); } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432608 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 (edited) You should use require_once() wherever you're using include. And you don't need to wrap your function in a function. Edit: Wait, is this JAVASCRIPT? wtf Edited May 27, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432609 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 You should use require_once() wherever you're using include. And you don't need to wrap your function in a function. Edit: Wait, is this JAVASCRIPT? wtf Yes. How else should I use that if statement? Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432611 Share on other sites More sharing options...
Strider64 Posted May 27, 2013 Share Posted May 27, 2013 Well, you don't mix Javascript with PHP.....might I suggest http://us2.php.net/manual/en/control-structures.if.php ? Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432614 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 Your original post showed no javascript, said nothing about javascript...That isn't even a valid javascript function, so there's absolutely no way that "Worked" before. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432616 Share on other sites More sharing options...
ady_bavarezu89 Posted May 27, 2013 Author Share Posted May 27, 2013 (edited) Your original post showed no javascript, said nothing about javascript...That isn't even a valid javascript function, so there's absolutely no way that "Worked" before. Yeah I said that I just added that code but I just noticed what bullshit I wrote!! Edited May 27, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432619 Share on other sites More sharing options...
ady_bavarezu89 Posted May 28, 2013 Author Share Posted May 28, 2013 I found an awesome solution :D!! First of all thanks Jessica for your help even though I didn't managed to implement it. I will post my solution here maybe some others will need something like this. So after I log in successfully I write into .txt file $handle = fopen('a.txt', 'w'); fwrite($handle, $user_id); fclose($handle); This way the .txt file always contains my session number and I can use it whenever I like When I need the data I simply write a select statement from my database, and pass in as the ID the session from my .txt file $file_handle = fopen("../a.txt", "r"); while (!feof($file_handle)) { $sess = fgets($file_handle); $loggedin_username = getFieldFromDB('username', $sess); echo "var '$loggedin_username'"; } fclose($file_handle) $sess stores the value from the .txt and will pass it in a method that will select the data I need. Works perfectly Thanks a lot again and I guess this can be marked as solved Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432762 Share on other sites More sharing options...
Jessica Posted May 28, 2013 Share Posted May 28, 2013 ... I think you should do some basic research on how a SESSION is stored. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432765 Share on other sites More sharing options...
ady_bavarezu89 Posted May 29, 2013 Author Share Posted May 29, 2013 (edited) ... I think you should do some basic research on how a SESSION is stored. Well it's really easy to say....but I am completely self taught an my website is kinda complicated combining bootstrap, php, jquery and javascript. I tried anything around sessions for like 3 days...also I tried to implement your solution but couldn't so I guess I have to live with that for now. Edited May 29, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1432995 Share on other sites More sharing options...
Jessica Posted May 29, 2013 Share Posted May 29, 2013 How on earth will your solution work? It doesn't even make any sense. What will happen when two users are logged in? Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433024 Share on other sites More sharing options...
ady_bavarezu89 Posted May 29, 2013 Author Share Posted May 29, 2013 How on earth will your solution work? It doesn't even make any sense. What will happen when two users are logged in? well...shit! Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433044 Share on other sites More sharing options...
DaveyK Posted May 29, 2013 Share Posted May 29, 2013 You realize PHP has a function that returns the session id? In addition, it is STORED in the current _SESSION... You are overcomplicating things. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433047 Share on other sites More sharing options...
ady_bavarezu89 Posted May 29, 2013 Author Share Posted May 29, 2013 You realize PHP has a function that returns the session id? In addition, it is STORED in the current _SESSION... You are overcomplicating things. Yeah I know that but like I said before I couldn't get it together yet...I just have to try again Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433050 Share on other sites More sharing options...
ady_bavarezu89 Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) Okay so I've done it as you guys told me. I'm just calling my login_form() only if I submitted the form. That way I won't have to access a.php from b.php. But it still doesn't work. The thing is when I submitted my form and the login was successful, I set my session variable: $user_id = mysql_result($query_run, 0, 'uid'); $_SESSION['user_id'] = $user_id; So for example if the first user in my DB is logging in, $_SESSION['user_id'] will store 1. After I go to b.php if I want to access that variable, let's say: echo $_SESSION['user_id']; I can do it only if I start the session on b.php right? session_start(); echo $_SESSION['user_id']; As far as I know session variables are worth nothing unless I start the session on that page. But if I go multiple times to that page, the session will start over and over again so that the user_id becomes uninitialized again and from the second time on I go to b.php I get this: Notice: Undefined index: user_id in... I've tried to set up a session counter or check if the session is started but even so you still have to start the session everytime you want to access that variable. Isn't there any way to escape this? To start the session only once and access the variables from then on without starting it again? Edited May 30, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433208 Share on other sites More sharing options...
Jessica Posted May 30, 2013 Share Posted May 30, 2013 You must call session_start() on any page where you want to use the session. If you don't see your variable in the session, then either you unset it or you did not set it. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433210 Share on other sites More sharing options...
ady_bavarezu89 Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) You must call session_start() on any page where you want to use the session. If you don't see your variable in the session, then either you unset it or you did not set it. The thing is I've set it...but I've set it on a.php...and when I go to b.php and access it I can see it. But if I go to c.php for example and then back to b.php it becomes null again because I call session_start() again, so I unset the variable like you said. But is there a way out of this? Can't I just store the session permanently and switch pages without unsetting my session variables? Edited May 30, 2013 by ady_bavarezu89 Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433211 Share on other sites More sharing options...
Jessica Posted May 30, 2013 Share Posted May 30, 2013 it becomes null again because I call session_start() again, so I unset the variable like you said. That is NOT what session_start() does. Quote Link to comment https://forums.phpfreaks.com/topic/278442-variables-initialize-everytime-i-go-to-a-page/#findComment-1433213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.