Jump to content

[SOLVED] Assign table value as a session??


tberger

Recommended Posts

When someone logs into my application - a login function runs from a pinc file.  What is suppose to happen is if the username and password match - it is suppose to assign a session to the username - which is being passed from form and to the first and last name which are in the table for the user.  Can I assign a session variable to value in a mysql table?  It does properly set the cookie I am assigning on the setCookie line but is is not setting any of the three sessions.  Here is the function I am using:

 

session_start();

function getLogin($username, $pass)

{

$username = trim($_POST['email_address']);

$pass = trim($_POST['password']);

 

$db=mysql_connect('monet.homeip.net','conn1','conn1') or die ("Cannot connect to server");

mysql_select_db('test',$db) or die ("Cannot select DB");

 

// Mysql_num_row is counting table row

$result=mysql_query("SELECT * FROM seebergerLogin WHERE username='$username' and password='$pass'");

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched username and password, table row must be 1 row

 

if($count==1){

 

// Register $username, $pass

$_SESSION['username'] = $username;

        $_SESSION['fName'] = $first_name;

        $_SESSION['lName'] = $last_name;

setcookie ("ok", $username);

return TRUE;

}

else

{

echo "Wrong Username or Password";

}

}

?>

 

 

I am sure I am missing something simple (at least I hope that is all) I appreciate any help you can offer.

 

TJ

Link to comment
Share on other sites

Ok so I tried this:

 

if($count==1){

// Register $username, $pass

$row = mysql_fetch_array($result) or die(mysql_error());

 

$_SESSION['username'] = $username;

        $_SESSION['fName'] = $first_name;

        $_SESSION['lName'] = $last_name;

setcookie ("ok", $username);

return TRUE;

}

else

{

echo "Wrong Username or Password";

}

}

 

On the welcome page where I am calling in these sessions, it is still not displaying the firstname and last name.  The username does work in both the session and the setcookie.

 

Here is where it is being called:

 

require('library.pinc');

$cookie= $HTTP_COOKIE_VARS["ok"];

  if (!isset($cookie)){

  header("Cache-Control: no-cache, must-revalidate");

  }

echo "Welcome, ";

echo $_SESSION[ 'first_name'] . " " . $_SESSION['last_name'], "<br />";

 

echo "You have successfully logged in as ";

echo $_SESSION['username'], "<br />";

   

echo "Your first and last names were passed by session data; your username was stored in the cookie. ", "<br />";

echo "The cookie value is: ";

echo $cookie, "<br />";

 

I am thinking I am not fetching correctly - any suggestions??

Link to comment
Share on other sites

please use [ code] tags

 

<?php
$row = mysql_fetch_array($result) or die(mysql_error());
?>

is correct but you need to set the $first_name & $last_name so

 

for example just say the field name of the first name is first and the field name for the last name is last

 

change

<?php
$row = mysql_fetch_array($result) or die(mysql_error());
      $_SESSION['username'] = $username;
        $_SESSION['fName'] = $first_name;
        $_SESSION['lName'] = $last_name;

?>

 

to

<?php
$row = mysql_fetch_array($result) or die(mysql_error());

      $_SESSION['username'] = $username;
        $_SESSION['fName'] = $row['first'];
        $_SESSION['lName'] = $row['last'];
?>

 

 

 

*note doesn't it look nicer and easier to read in code tags

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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