Vise den bruger som er logget inds userId i tekstfeldt
Hvordan kan jeg få den bruger som er logget inds userId til at blive vist i et tekstfeldt som dette userId<input type="text" name="userId" value="<? echo $profile->userId; ?>">Man er logget ind før man kan se siden. Filen comon.php vises nederst.
<?
require_once("common.php");
session_start();
if($userId = is_logged())
{
$userName = get_name();
}
else
{
echo' <body scroll="no" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td width="100%">
<div align="center">
<img src="../NewFiles/ikkeLoggetInd.gif" alt="" border="0"><br>
</div>
</td>
</tr>
<tr>
<td width="100%">
<div align="center">
</div>
</td>
</tr>
</table>
</body> ';
}
if(!empty($_POST['produkt']))
{
//Forbind til database...
$userId = mysql_real_escape_string($_POST['userId']);
$produkt = mysql_real_escape_string($_POST['produkt']);
mysql_query("INSERT INTO salgAfProdukt(Id,userId, produkt) VALUES('','".$userId."','".$produkt."')");
if(mysql_affected_rows()>0)
{
echo "Felt indsat<br>";
}
else
{
echo "Fejl: " . mysql_error() . "<br>";
}
}
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
userId<input type="text" name="userId" value="<? echo $profile->userName; ?>"><br>
Produkt<input type="text" name="produkt"><br>
<input type="submit" value="Indsæt i DB">
</form>
FILEN COMMON.PHP
<?
require_once("../inc/config.php");
class UserProfile
{
var $id;
var $userName;
var $title;
var $firstName;
var $lastName;
var $company;
var $email;
var $addr1;
var $addr2;
var $city;
var $state;
var $country;
var $tel;
var $fax;
var $mobiTel;
var $homeTel;
var $web;
var $key;
var $IP;
var $signUp;
var $validated;
var $newsLetter;
}
/**
* shows a formatted error message
*/
function err_message($str)
{
echo sprintf('<table border=0 width="350" align="center">
<tr><td>%s</td></tr>
</table><br>',$str);
}
/**
* this function returns the currently logged in user's username
*/
function get_name()
{
global $con;
$sid = session_id();
$query = "SELECT a.userFirstName FROM userProfile a, loggedUsers b
WHERE b.sessionId = '$sid' and b.userId = a.userId";
$result = mysql_query($query);
if($result)
{
$row = mysql_fetch_row($result);
return $row[0];
}
else
{
return "";
}
}
/**
* Creates an entry in the logged users table. Call this method
* directly if you want to automatically log in a new user who
* has just signed up.
*/
function set_session($userId,$sessionId, $con)
{
$query = "insert into loggedUsers set userId = $userId,
sessionId = '$sessionId', loginTime = now(),
lastAccess = now()";
$result = mysql_query($query,$con);
if(mysql_errno() != 0)
{
/*
* it could be that you are already logged in
*/
$u2 = is_logged($sessionId);
return ($u2 == $userId);
}
return 1;
}
/**
* this should not be a function, it should be a cron. It has however
* been made available so that you have a means of cleaning up unwanted
* sessions, even if you do not have access to the cron daemon or other
* scheduling mechanism.
*/
function clean_sessions()
{
$query = "delete from loggedUsers where
unix_timestamp(date_add(lastAccess, interval 1 hour)) < unix_timestamp(now())";
$result = mysql_query($query);
}
/**
* returns 0 if you are not logged in. else returns your userid
* also updates the 'lastAccess' field in the logged users table.
*/
function is_logged($sid="")
{
global $con;
if(!isset($sid) || $sid == '')
{
$sid = session_id();
}
/*
* if you set up a cron to clean up unwanted sessions, please comment
* the next line.
*/
clean_sessions();
$query = "SELECT userId from loggedUsers where sessionId = '$sid' and
unix_timestamp(date_add(lastAccess, interval 1 hour)) > unix_timestamp(now())";
$result = mysql_query($query);
if($result)
{
$row = mysql_fetch_row($result);
if($row)
{
$query = "UPDATE loggedUsers set lastAccess=now() where userId = $row[0]";
mysql_query($query);
}
return $row[0];
}
else
{
return 0;
}
}
/**
* Are you logged in as the administrator?
* also updates the 'lastAccess' field in the logged users table.
*/
function is_admin($sid="")
{
global $con;
if(!isset($sid) || $sid == '')
{
$sid = session_id();
}
clean_sessions();
$query = "SELECT a.userId FROM loggedUsers a, users b
WHERE a.sessionId = '$sid' AND b.userStatus >= 2 AND
a.userId = b.userId AND
unix_timestamp(date_add(lastAccess, interval 1 hour)) > unix_timestamp(now())";
$result = mysql_query($query);
if($result)
{
$row = mysql_fetch_row($result);
if($row && $row[1]>1)
{
$query = "UPDATE loggedUsers set lastAccess=now() where userId = $row[0]";
mysql_query($query);
}
return $row[0];
}
else
{
return 0;
}
}
/**
* retrieves the uers's status. Currently supported values are
* 0 - disabled.
* 1 - enable.
* 2 - admin.
*/
function get_user_status($userId)
{
$query = "SELECT userStatus from users where userId = $userId";
$result = mysql_query($query);
if($result)
{
$row = mysql_fetch_row($result);
return $row[0];
}
return 0;
}
/**
* retrieves the email address given the username, used mainly by the
* password reminder service.
*/
function get_email($username, $userId=0)
{
if($userId==0)
{
$query = "SELECT a.userEmail from userProfile a, users b
where b.username='$username' and b.userId = a.userId";
}
else
{
$query = "SELECT userEmail from userProfile where
userId = $userId";
}
error_log($query);
$result = mysql_query($query);
if(mysql_errno() == 0)
{
if($result)
{
$row=mysql_fetch_row($result);
return $row[0];
}
else
{
return 0;
}
}
else
{
error_log(mysql_error());
return 0;
}
}
/**
* returns an instance of UserProfile for the member whose userId
* is passed in as a parameter.
*/
function get_profile($userId)
{
global $con;
$query = "SELECT * from userProfile where userId = $userId";
$result = mysql_query($query);
if($result)
{
$row = mysql_fetch_array($result);
$profile = new UserProfile;
$profile->id = $row['userId'];
$profile->firstName = $row['userFirstName'];
$profile->lastName = $row['userLastName'];
$profile->email = $row['userEmail'];
$profile->addr1 = $row['userAddr1'];
$profile->addr2 = $row['userAddr2'];
$profile->city = $row['userCity'];
$profile->state = $row['userState'];
$profile->country = $row['userCountry'];
$profile->tel = $row['userTel'] ;
$profile->mobiTel = $row['userMobiTel'] ;
$profile->homeTel = $row['userHomeTel'] ;
$profile->web = $row['userWeb'] ;
$profile->fax = $row['userFax'];
$profile->key = $row['userValidationKey'];
$profile->IP = $row['userIP'];
$profile->signUp = $row['userSignUp'];
$profile->validated = $row['userValidated'];
$profile->newsLetter = $row['userNewsLetter'];
/*
* this can be optimized so kill me
*/
$query = "SELECT userName FROM users WHERE userId = $userId";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$profile->userName=$row[0];
return $profile;
}
}
/**
* displays the box that allows the user to view/change his
* profile
*/
function show_profile($userId)
{
$profile = get_profile($userId);
require_once('profile.txt');
}
/**
* called in when the user submits the change profile form
*/
function change_profile($profile)
{
global $con;
$query = sprintf("UPDATE userProfile SET userFirstName='%s',
userLastName='%s', userAddr1 = '%s', userAddr2 = '%s',
userEmail = '%s', userTel = '%s', userFax = '%s',
userWeb = '%s', userMobiTel = '%s', userHomeTel = '%s',
userZip = '%s', userCountry = '%s', userState = '%s',
userCity= '%s' WHERE userId = %s",
$profile->firstName, $profile->lastName,
$profile->addr1, $profile->addr2, $profile->email,
$profile->tel, $profile->fax, $profile->web,
$profile->mobiTel, $profile->homeTel,
$profile->zip,$profile->country,
$profile->state,$profile->city,$profile->id);
$result = mysql_query($query);
return mysql_errno();
}
/**
* subscribe/unsubscribe from newsletters
*/
function change_newsletter($userId,$setting)
{
global $con;
$val = 0;
if($setting == 'yes')
{
$val=1;
}
$query = "UPDATE userProfile set userNewsLetter=$val where userId=$userId";
mysql_query($query);
return mysql_errno();
}
/**
* changes the password for the given user
*/
function change_password($userId,$password)
{
global $con;
$password = addslashes($password);
$query = "UPDATE users set userPassword= password('$password') WHERE userId=$userId";
$result = mysql_query($query);
return mysql_errno();
}
/**
* returns true if the username and password, and password confirm fields
* are set.
*/
function is_valid_username()
{
$pass = sanitize_variable($_REQUEST['password']);
$pass1 = sanitize_variable($_REQUEST['password1']);
$user = sanitize_variable($_REQUEST['username']);
return (isset($pass) && $pass != '' &&
isset($pass1) && $pass1 != '' &&
isset($user) && $user != '');
}
/**
* this method changes the user status. The acceptable values are
* 0 - disable account
* 1 - enable account
* 2 - mark as admin
*/
function set_user_status($userId, $status)
{
$query = "UPDATE users set userStatus = $status WHERE userId = $userId";
return mysql_query($query);
}
function sanitize_variable($var)
{
return addslashes(trim(strip_tags($var)));
}
/**
* Nedenfor kan HOTMAIL og andre webmails slås fra..
*/
function is_valid_addr()
{
$disallow = "/hotmaillll\.com|msssn\.com|yahooo\.com|bigoooot\.com|lycooos\.com/";
$email = sanitize_variable($_REQUEST['email']);
if($email == '' || preg_match($disallow,$email))
{
return 0;
}
else
{
return 1;
}
}
/**
* returns userId on success. 0 on failure.
*/
function is_valid($user,$password)
{
$query = "SELECT userId FROM users WHERE
userName = '$user' and userPassword = password('$password') and userStatus > 0";
$result = mysql_query($query);
error_log(mysql_error());
if($result && mysql_num_rows($result) ==1)
{
$row = mysql_fetch_row($result);
return $row[0];
}
return 0;
}
/**
* check the referer to minimize abuse..
* todo: a more vigourous check.
*/
function is_valid_referer()
{
global $site_url;
return (strstr($_SERVER['HTTP_REFERER'],$site_url));
}
function on_session_start($save_path, $session_name) {
error_log($session_name . " ". session_id());
}
function on_session_end() {
// Nothing needs to be done in this function
// since we used persistent connection.
}
function on_session_read($key) {
error_log($key);
$stmt = "select session_data from sessions ";
$stmt .= "where session_id ='$key' ";
$stmt .= "and unix_timestamp(session_expiration) > unix_timestamp(date_add(now(),interval 1 hour))";
$sth = mysql_query($stmt);
if($sth)
{
$row = mysql_fetch_array($sth);
return($row['session_data']);
}
else
{
return $sth;
}
}
/**
* The heart of the session manager.
*
* If you are load balancing your web site across several servers you cannot
* store session information in files. You will either need to store the
* information in a database or use cookies. Since many people are reluctant
* to trust cookies your choices narrow down to exactly one. YOu need to use
* database.
*
* Storing session information in a database makes sense if you are on a
* shared hosting enviorenment and have concerns about security.
*
* To enabale this feature set the variable $session_in_db to 'db';
*/
function on_session_write($key, $val) {
error_log("$key = $value");
$val = addslashes($val);
$insert_stmt = "insert into sessions values('$key', ";
$insert_stmt .= "'$val',unix_timestamp(date_add(now(), interval 1 hour)))";
$update_stmt = "update sessions set session_data ='$val', ";
$update_stmt .= "session_expiration = unix_timestamp(date_add(now(), interval 1 hour))";
$update_stmt .= "where session_id ='$key '";
// First we try to insert, if that doesn't succeed, it means
// session is already in the table and we try to update
mysql_query($insert_stmt);
$err = mysql_error();
if ($err != 0)
{
error_log( mysql_error());
mysql_query($update_stmt);
}
}
function on_session_destroy($key) {
mysql_query("delete from sessions where session_id = '$key'");
}
function on_session_gc($max_lifetime)
{
mysql_query("delete from sessions where unix_timestamp(session_expiration) < unix_timestamp(now())");
}
error_log('=--------------- in session php' . $session_save . '-------------');
if($session_save == 'db')
{
error_log('setting save handler');
// Set the save handlers
session_set_save_handler("on_session_start", "on_session_end",
"on_session_read", "on_session_write",
"on_session_destroy", "on_session_gc");
}
session_start();
?>
