Jump to content

ady_bavarezu89

Members
  • Posts

    17
  • Joined

  • Last visited

Everything posted by ady_bavarezu89

  1. Got it! I didn't knew that. Thanks a lot for the suggestions and everything
  2. This time it finally works. It stores my session and more users can be logged in at the same time. Finally!!
  3. I think I found my problem....On my page b.php I also had a logout option. Which means that if I press a button my session is destroyed. and I did it that way: <button class="btn btn-primary" type="submit" onclick="myFunction()"> </button> <script> function myFunction(){ <?php session_destroy(); ?> } </script> So php is interpreting my php code even if it's inside a javascript function that wasn't called. That's why that guy warned me about using javascript and php!
  4. it is not? why is user_id null (Notice: Undefined index: user_id in) when I go to b.php the second time then? The first time it shows 1 as it should but from the second time it becomes undefined.
  5. 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?
  6. 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?
  7. Yeah I know that but like I said before I couldn't get it together yet...I just have to try again
  8. 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.
  9. 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
  10. Yeah I said that I just added that code but I just noticed what bullshit I wrote!!
  11. 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>
  12. 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(); }
  13. 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?
  14. 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?
  15. 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!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.