This ought to be so obvious and essential, but I don't understand how I retrieve and update SQL data for a logged-in user with php SESSION.
I have searched and read, but the tutorials only explain how I register and start a session for a user, but not the most important part: How do I extract SQL data for this user, and update his data in the SQL database?
I have made a registration form and a login page. Now: How do I know who is logged in, and how do I read and write data to and from my SQL database for this user?
For a start, I want to put the user's name on the welcome page, after the user logged in. I do not know which user it is, or what his or her name is, so I need to pull out the name from my SQL database and display the user's name on the welcome page. How do I do that with php SESSION?
Here is my script from login.php registering the session:
if(isset($_POST['login'])) {
if($signature == "" | $userpw == "") {
$notice = "<br><i><font color='#B80000;'><i>Please enter signature and password.</font></i><br>";
$onload = " onload='document.loginForm.signature.focus()';";
}
else if(preg_match("/^[A-Za-zÆØÅæøå -]+$/",$signature)) {
if(checkLogin($signature,$encpw)) {
if(verifyAccount($signature)) {
session_start();
session_register('user');
header("Location: welcome.php");
function checkLogin($sig,$pass) {
include("archive/file.inc");
$connection = mysql_connect($host, $account, $password)
or die("Error: ".mysql_error());
$db = mysql_select_db($dbname, $connection);
$query = "SELECT * FROM users WHERE signature = '$sig' AND userpw = '$pass'"
or die("Error: ".mysql_error());
$result = mysql_query($query)
or die("Error: ".mysql_error());
mysql_close($connection);
if(mysql_num_rows($result)) {
return true;
}
else return false;
}
function verifyAccount($testSig) {
include("archive/file.inc");
$connection = mysql_connect($host, $account, $password)
or die("Error: ".mysql_error());
$db = mysql_select_db($dbname, $connection);
$query = "SELECT * FROM users WHERE signature = '$testSig' AND verified = 1"
or die("Error: ".mysql_error());
$result = mysql_query($query)
or die("Error: ".mysql_error());
mysql_close($connection);
if(mysql_num_rows($result)) {
return true;
}
else return false;
}
The login.php page is tested and working. Now, after login, I take the user to the welcome.php page. Here is my script for that page:
<?php
session_start();
if(!session_is_registered('user')) {
header("Location: login.php");
}
$logged_in = 1;
echo "
<html>
<head>
<title>Welcome</title>
<script language='javascript'>
welcome_bw = new Image();
welcome_rgb = new Image();
welcome_bw.src = 'images/welcome_bw.jpg';
welcome_rgb.src = 'images/welcome_rgb.jpg';
";
include("archive/file.inc");
$connection = mysql_connect($host, $account, $password)
or die("Error: ".mysql_error());
$db = mysql_select_db($dbname, $connection);
$query = "SELECT * FROM users WHERE user = '$_SESSION[user]'"
or die("Error: ".mysql_error());
$result = mysql_query($query)
or die("Error: ".mysql_error());
mysql_close($connection);
$row = mysql_fetch_array($result);
$name = $row['name'];
include("begin_page.inc");
echo "
<img border='0' src='graphics/welcome.gif' width='140' height='45' alt='Welcome'><br>
<i>Welcome, ".$name."</i><br>
";
include("end_page.inc");
?>
The code above does not generate any error, but no name is displayed for the user, either.
How do I pull out the user's name after login, and how can I let the user update e.g. his e-mail address and update that information for that user in my SQL database accordingly? What am I missing?
This must be essential for php SESSION, but it is a big mystery to me, and not explained very well anywhere. How come?
Thank you very much in advance for your help.