Question:

Question on php sessions?

by  |  earlier

0 LIKES UnLike

when you start a session, you start it with a variable... right?

for example i could name the variable "Bob" or "Joe" etc.

though when the sessions started, and i call that variable on another page after "Welcome:"

it'll say "Welcome Bob" ... or Joe.. etc.

well. on my site, users log in by entering their email and password.

[when they registered, they had to include their first name, last name, email, password, country, region, and age..]

so.. when they log in.. how do i start a session that takes their "first name".. not their email or anything.. just their first name that they entered, and use it as a variable.

per se:

how do i start a session with something in mySQL database.. something that they don't need to enter, it just calls it from where it's grabbing the email address and the password...

so that it'll say "Welcome: "users name""

i'm having bad problems with this and no one's been able to help me.. so please explain this to me as though i'm a 5 year old.. don't just straight out say "you have to do this"

because "doing this" doesn't quite work..

what exactly do i do, what type of coding with php.. and what does each of these codes do?

Thanks

 Tags:

   Report

3 ANSWERS


  1. I am trying to explain you the situation.

    First Step:

    session should be started on every page. So better solution is, to put your session start code in the header of your files.

    Note: It should be at the start of every page.

    Example:

    <?php

    session_start();

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...

    .........................

    .........................

    Second Step:

    When user authenticate by entering email and password. you can write query like this:

    $query = "SELECT * FROM USER WHERE `email`='email' AND `password`='password' LIMIT 1";

    Note: Query will retreive full row of this particular user, including its other details like , firstname, username etc

    Now you should put username in session basket like.

    $_SESSION['sess_username'] = $row['username'];

    Third Step:

    You can print his name at every page like:

    echo "Hello " . $_SESSION['sess_username'];

    Free PHP resource

    http://www.rapidsharehub.com/tag-php-Pag...

    ========================

    Additional Detail Solution

    ========================

    First of all:

    I again advice you to start session at the top of page. e.g in your case example : it should be

    <?php

    session_start();

    include("config.php");

    .........................................

    Your else statement should be

    else {

    Possible fixes in your else block:

    ===============================

    $query = "SELECT * FROM $tbl_name1 WHERE `email`='".$_POST['email']."."' AND `password`='".$_POST['password']."."' LIMIT 1";

    $result = mysql_query($query);

    $row = mysql_fetch_array($result, MYSQL_ASSOC);

    $_SESSION['sess_name'] = $row['name'];

    mysql_free_result($result);

    .........................................

    Useful Tip:

    =================================

    You should be test and test again any value before assigning to the session. e.g

    In your case you are doing this one,

    $_SESSION['sess_name'] = $row['name'];

    So make sure, Value is coming in $row['name'];

    Than after your satisfied test, assign it to session.

    Session is so simple logic. Start on the top of every page. and It should work. If some value is not appearing in via session, than it can be logic error.

    You can tweak your code as best as possible but with the passage of time and experience ;)

    Free IT Books Resource

    http://www.rapidsharehub.com/tag-free-it...


  2. your code is now OK, all you need to do is to redirect your page to index2.php (don't use anchor tag to redirect ( a href="" )

    heres the code to redirect:

    else

    {

    session_start();

    $query = "SELECT * FROM USER WHERE `email`='email' AND `password`='password' LIMIT 1";

    $_SESSION['sess_name'] = $row['name'];

    header("Location: index2.php");

    } // end else

    then on your index2.php, make sure you dont miss a closing php tag (  ?> )

    <?php

    session_start();

       echo "Welcome " . $_SESSION['sess_name'];

    ?>

    hope this helps.

  3. You're already adding the form data into the database right?  So you know how to take it out via a MySQL query?  Next, you'll take the query result and save it to the variable...  

    One thing you may be missing is that you need to start the session on every page.  The first time the user navigates to a page that doesn't use session_start() the session and its variables is lost.

    Try a simpler example of sessions first, just to make sure your PHP is setup to use session variables.  If this works and you're still having problems you can email me and I'll look at it when I wake up, if it doesn't you may need to set php to allow session variables.

    Make two pages.

    --first.php--

    <?php

    session_start();

    $_SESSION['name'] = "Bob";

    ?>

    <a href="second.php">Go to second page</a>

    --

    --second.php--

    <?php

    session_start();

    echo "Welcome " . $_SESSION['name'] . "!";

    ?>

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.