Jump to content

Variables initialize everytime I go to a page


ady_bavarezu89

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

...

 

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.

Link to comment
Share on other sites

...

 

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?

 

           

Link to comment
Share on other sites

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();

}
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

I found an awesome solution :D: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 :D

 

Thanks a lot again and I guess this can be marked as solved

Link to comment
Share on other sites

... 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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.