Question:

Sessions in php and mySQL?

by  |  earlier

0 LIKES UnLike

No one seems to know how to do this.. and I NEED to know how to do this.

On my site, the person registers, they login.

As soon as they click login after entering their email and password.

It grabs their data from the members database, and pretty much logs them in.

In order to keep them logged in basically, I of course would start a session.

i'm not that great with sessions.. i know how to set a variable on one page in a session, start a session on the next page, and read the variable..

so if i name the variable 'name' and make it equal to 'Jack', and read the variable on the next page, "Jack" will show up.

okay.. but i'm not trying to do that because if someone logs in.. they're not always going to be Jack.. they're going to be wahetver they entered their name as when they first registered to become a member.

So..

here.. look at the code i'm using..

on the login.php page, that's where the enter their email and password to login, which gets sent to the login2.php page.

login2.php

--------------

<?php

include("config.php");

$tbl_name1="registered_members";

$match = "select id from $tbl_name1 where email = '".$_POST['email']."'

and password = '".$_POST['password']."';";

$qry = mysql_query($match)

or die ("Could not match data because ".mysql_error());

$num_rows = mysql_num_rows($qry);

if ($num_rows <= 0) {

echo "Sorry, there is no email $email with the specified password.<br>";

echo "<a href=login.php>Try again</a>";

exit;

} else {

session_start();

$_SESSION['name'] = $name;

include("index2.php");

}

?>

it includes config.php in which is what starts mysql ordeal.

it grabs the id's from the registered memebers table "email" and "password" and it basically confirms that they're there..

then after that.. i start my session to keep them logged in.

you can see how i started my session.

you can then see that they're redirected (basically) to index2.php [which for now is the members area until i can fix my problem]

i now have a page called logged.php, the coding for it is:

<?php

session_start();

echo $_SESSION['email']

?>

and on index2.php [the supposed redirect page[members area]]

i have "Welcome <?php include("logged.php");?>

so that it continues the session from the login2 page and comes up with "Welcome 'users name'"

okay.. now see.. that doesn't work.

i'm basically telling you what my idea and my goal is.. but that's not working.

i tried using the whole:

select id from $tbl_name1 where name

so that it'll call from the same table.. but then i get an error.

so i tried to go without it, and in the starting session, i replaced both "name"s with "email" so that maybe it would say "Welcome 'users email'" in which it doesn't do that... but it atleast comes up with index2 without giving me an error.. but it just says "Welcome" and that's it..

what do i do?

how do i have a user enter their email and password to login, and once done, have them get redirected to the members area [index2.php] and have it say "Welcome 'their name'"

how does it grab the name they entered during registration from the database in the process of login them in with their email and password..

does ANYONE know?!?!

 Tags:

   Report

3 ANSWERS


  1. When you include a php file in your current script it&#039;s included, not processed separately, thus it&#039;s still within the same page and the current page hasn&#039;t finished processing.

    Thus, session is not set yet. This is the expected behavior.

    If you need to load a page after setting session data, you should set session data and then send a redirection or refresh header (remember not to send anything, not even whitespace before sending headers).

    Always consider session data to be updated after the next page load (as in http request completed).

    To clarify, in login2.php you are including index2.php at the bottom, where you should be redirecting the page to index2.php upon user login.

    Edit:

    Lucys method will work, but it is incredibly unsecure.


  2. W3SCHOOLS.COM

    PHP.NET

    GO THERE  

  3. The way that I do that is to log the session ID and UserID associated with that session ID in a table.  Then I add the session ID at the end of the URL for all subsequent pages so that I can use it as a variable to look up the user information by joining the UserID in the session log table with the table that I have with the name associated with the UserID.  Hope this helped!

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.