Jump to content

Retrieving further info


Go to solution Solved by mac_gyver,

Recommended Posts

In my script, users login with their Username & Password. However, I'd like to be able to echo the email address used on their account.

I've tried adding the email to the session
I'm not having much luck...

Here's a piece of the login code(untouched);

                $username = $_POST['name'];
                $passwd = $_POST['passwd'];
                $query = "SELECT name,passwd FROM users WHERE CONCAT('0x', hex(passwd)) = '{$salt}'";
		$result = mysql_query($query);
		$login_ok = false;
		if(mysql_num_rows($result) > 0) {
		    $login_ok = true;
		}
		if($login_ok)
		{
		    $row = mysql_fetch_array($result, MYSQL_NUM);
		    $_SESSION['user'] = $row;

I've also tried messing around with this piece below in a few different ways but still nothing.

<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>

Any help is greatly appreciated..

Link to comment
https://forums.phpfreaks.com/topic/288920-retrieving-further-info/
Share on other sites

I was showing a snippet of the Login authentication process I'm using in hopes someone could help me add to that first created session the users email too.

I have tried this;

 

<?php
$query= mysql_query("SELECT * FROM `users` WHERE `name` = '".$_SESSION['user']."' ")or die(mysql_error());
$account = mysql_fetch_array($query);
$num = mysql_numrows($query);
?>
///
<?php echo $account['email']; ?>


But the results display empty :s

  • Solution

in the first posted code, you are fetching a numerical array - $row = mysql_fetch_array($result, MYSQL_NUM);, so even if your sql query in that code was selecting the email field, it wouldn't be available using the associative name 'email'.

 

in the last posted code, $_SESSION['user'] contains the numerically indexed array $row from your first posted code. $_SESSION['user'][0] would contain the name.

 

so, if you don't expect the name value to ever get changed for anyone while they are logged in, change your first code to also select the name field in the sql query and use mysql_fetch_assoc() to fetch an associative array, where you could reference the ['name'] or ['email'] fields in your code using $_SESSION['user']['name'] or $_SESSION['user']['email']

 

finally, the mysql_ functions are depreciated, you should be switching to mysqli or PDO database functions.

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.