Jump to content


Photo

get user id of user using web php page


  • Please log in to reply
18 replies to this topic

#1 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 06:11 PM

how to get user id of user using php server side web page.
I need to add it to my web page and use it to autofill the "Sent from" for php email.

Username: [  ]

thanks in advance

Mike

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 23 August 2006 - 06:18 PM

You will either have to pass it on thru hidden form variables or you can use session or cookies.

Personally I have used sessions. To use sessions you would have to start EVERY one of your pages with
session_start();

then you would assign the session value
$_SESSION['user'] = $_POST['username'];

once that is assigned you can call on the username at anytime by calling
$_SESSION['user']

Ray

#3 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 06:30 PM

Thanks Cratgo :-)

I tried that (novice user). but got the follow message...

===============
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at E:\Techassist\RMA\sendForm.php:6) in E:\Techassist\RMA\sendForm.php on line 141

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at E:\Techassist\RMA\sendForm.php:6) in E:\Techassist\RMA\sendForm.php on line 141
===============

Where is the best place to put this function ? (please excuse my ignorance, i'm new to php)


You will either have to pass it on thru hidden form variables or you can use session or cookies.

Personally I have used sessions. To use sessions you would have to start EVERY one of your pages with

session_start();

then you would assign the session value
$_SESSION['user'] = $_POST['username'];

once that is assigned you can call on the username at anytime by calling
$_SESSION['user']

Ray



#4 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 06:32 PM

make sure that session_start() is at the very beginning of your script, before any html code is sent to the browser.
What is the CoronaMatrix?

#5 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 23 August 2006 - 06:38 PM

either that or start your script by calling ob_start();... I use it, some people think it's ghetto I don't see the problem though...

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 23 August 2006 - 06:50 PM

Line 1 on your page should be <?
Line 2 on your page should be session_start();

continue with your code...

#7 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 06:58 PM

Here's my code... still has empty field for username:
what am i doing wrong ?

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

<?php
//to obtain user id
session_start();

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php

function sendForm()
{

//ob_start();

$_SESSION['user'] = $_POST['username'];

$formData='

<table width="400" border="1" cellspacing="1" bordercolor="#D40139">
  <tr>
    <td>
		User: <input name="txtUser" type="text" value="'.$_SESSION['user'].'" readonly="true">
	</td>
	</tr>
</table>
';

//Send the completed form
sendForm();
?>
<body>
</body>
</html>



#8 rallokkcaz

rallokkcaz
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationSomewhere, CA (Cactus Area)

Posted 23 August 2006 - 07:15 PM

first of all are you even logged in?
second can you login through the login.php(.html .htm ext.)

that might help the problem

#9 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 07:21 PM

no not formally logging onto the site. but logging onto domain
I was hoping to grab the windows user id even from local environment vars.
Seems like such a task but definately valuable on my form.

Thanks for all the responses so far :-)


first of all are you even logged in?
second can you login through the login.php(.html .htm ext.)

that might help the problem



#10 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 23 August 2006 - 07:22 PM

here is some working code. You could eventually change the username form field to a dropdown menu later. But anyway

<?php
session_start();
header("Cache-Control: private");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
//ob_start();

$_SESSION['user'] = $_POST['username'];
echo $_SESSION['user'];
} else {
?>
<form name=form action="<?=$_SERVER['PHP_SELF']?>" method=POST>
<table width="400" border="1" cellspacing="1" bordercolor="#D40139">
  <tr>
    <td>
		User: <input name="username" type="text" value="">
	</td>
    <td>
		<input name="submit" type="submit" value="Submit">
	</td>
	</tr>
</table>
</form>
<?
}
?>
<body>
</body>
</html>


#11 rallokkcaz

rallokkcaz
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationSomewhere, CA (Cactus Area)

Posted 23 August 2006 - 07:23 PM

well if you try to login in to the site through
sed login page then it'll probly work!  :D

#12 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 23 August 2006 - 07:25 PM

<?php
//to obtain user id
session_start();

?>

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

You need it at the very top.
What is the CoronaMatrix?

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 August 2006 - 07:56 PM

echo $_SERVER['LOGON_USER'];    // --> domain\username

If you are using IIS you will have to disable anonymous logins, either at wwwroot level for the site or for the folder your app is in.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 08:08 PM

Thank you

Tried the below as well ... still an empty username field.
But the below code looked promising... is there something else I need to do for it to work ?

Also tried the other suggestions by other members... you're all brilliant thanks again for the replies :-)

Mike

echo $_SERVER['LOGON_USER'];    // --> domain\username

If you are using IIS you will have to disable anonymous logins, either at wwwroot level for the site or for the folder your app is in.



#15 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 08:11 PM

Here the full code... all works fine except for that username field...
isn't it always the last part of the project that seems to be the hardest ?...lol

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
//to obtain user id
session_start();

?>
<html>
<head>
<title>Corporate RMA Request Form - Submit Form</title>

<?php

function sendForm()
{
//ob_start();

$_SESSION['user'] = $_SERVER['LOGON_USER'];    // --> domain\username

//form vars
$newLine = "<br>";

//populate vars from post

$txtDate = $_POST["txtDate"];
$txtAgentName=$_POST["txtAgentName"];
$txtBanNum=$_POST["txtBanNum"];
$txtWirelessNum=$_POST["txtWirelessNum"];
$txtCustomerName=$_POST["txtCustomerName"];
$txtTicketNum=$_POST["txtTicketNum"];
$txtContactName=$_POST["txtContactName"];
$txtContactPhone=$_POST["txtContactPhone"];
$txtContactEmail=$_POST["txtContactEmail"];
$drpWirelessDevice=$_POST["drpWirelessDevice"];
$txtPinNum=$_POST["txtPinNum"];
$txtIMEI=$_POST["txtIMEI"];
$txtSimNum=$_POST["txtSimNum"];
$txtDefectNote=$_POST["txtDefectNote"];
$txtShipAttentionTo=$_POST["txtShipAttentionTo"];
$txtShipCompanyName=$_POST["txtShipCompanyName"];
$txtShipAddress=$_POST["txtShipAddress"];
$txtShipCity=$_POST["txtShipCity"];
$txtShipProv=$_POST["txtShipProv"];
$txtShipPostal=$_POST["txtShipPostal"];


$formData='

<table width="200" border="1" cellspacing="1" bordercolor="#D40139">
    <tr>
		<td width=300 valign=top bgcolor="#D40139" align="right">
			<b><span style="font-size:9.0pt;color:white">Corporate RMA</span></b>
		</td>
		<td width=300 valign=top bgcolor="#D40139" align="left"><b><span style="font-size:9.0pt;color:white">Request form</span>
		</td>
    </tr>
  <tr>
    <td>
<div align="right">
			Date: <br>
			RMA #: <br>
			User: <br>
</div>
	</td>
	<td>
		<input name="txtDate" type="text" value="'.$txtDate.'" readonly="true"><br>
		<input name="txtShowTicketNum" type="text" value="'.$txtTicketNum.'" readonly="true"><br>
		<input name="txtUser" type="text" value="'.$_SESSION['user'].'" readonly="true">
	</td>
	</tr>
    <tr>
		<td width=300 valign=top bgcolor="#D40139">
			<b><span style="font-size:9.0pt;color:white">Customer Information</span></b>
		</td>
		<td width=300 valign=top bgcolor="#D40139">&nbsp;
		</td>
    </tr>
  <tr>
    <td>
<div align="right">
		Ban: <br>
		Wireless #: <br>
		Customer Name: <br>
</div>
	</td>
	<td>
		<input name="txtBanNum" type="text" value="'.$txtBanNum.'" readonly="true"><br>
		<input name="txtWirelessNum" type="text" value="'.$txtWirelessNum.'" readonly="true"><br>
		<input name="txtCustomerName" type="text" value="'.$txtCustomerName.'" readonly="true">
	</td>
	</tr>
    <tr>
		<td width=300 valign=top bgcolor="#D40139">
			<b><span style="font-size:9.0pt;color:white">Contact Information</span></b>
		</td>
		<td width=300 valign=top bgcolor="#D40139">&nbsp;
		</td>
    </tr>
  <tr>
    <td>
<div align="right">
		Contact Name: <br>
		Contact Phone: <br>
		Contact Email: 
</div>
	</td>
	<td>
		<input name="txtContactName" type="text" value="'.$txtContactName.'" readonly="true"><br>
		<input name="txtContactPhone" type="text" value="'.$txtContactPhone.'" readonly="true"><br>
		<input name="txtCustomerName" type="text" value="'.$txtContactEmail.'" readonly="true">
	</td>
  </tr>
    <tr>
		<td width=300 valign=top bgcolor="#D40139">
			<b><span style="font-size:9.0pt;color:white">Device Information</span></b>
		</td>
		<td width=300 valign=top bgcolor="#D40139">&nbsp;
		</td>
    </tr>
  <tr>
    <td>
<div align="right">
		Pin: <br>
		IMEI: <br>
		Sim Card #: 
</div>
	</td>
	<td>
		<input name="txtPinNum" type="text" value="'.$txtPinNum.'" readonly="true"><br>
		<input name="txtIMEI" type="text" value="'.$txtIMEI.'" readonly="true"><br>
		<input name="txtSimNum" type="text" value="'.$txtSimNum.'" readonly="true">
	</td>
  </tr>
    <tr>
		<td width=300 valign=top bgcolor="#D40139">
			<b><span style="font-size:9.0pt;color:white">Shipping Information</span></b>
		</td>
		<td width=300 valign=top bgcolor="#D40139">
			<b><span style="font-size:9.0pt;color:white">Defect Information</span></b>
		</td>
    </tr>
  <tr>
    <td>
		<textarea name="txtAddressFull" cols="35" rows="6" readonly="true">'.$txtShipAttentionTo.'
'.$txtShipCompanyName.'
'.$txtShipAddress.'
'.$txtShipCity.' '.$txtShipProv.' '.$txtShipPostal.'
		</textarea>
	</td>
    <td>
		<textarea name="txtDefectNote" cols="35" rows="6" readonly="true">'.$txtDefectNote.'</textarea>
	</td>
  </tr>
</table>
';

//populate email vars
echo $newLine;
$to = "michael.webster@rci.rogers.com";
//$headers = "From: michael.webster@rci.rogers.com";
$headers = "From: michael.webster@rci.rogers.com\r\nContent-type: text/html\r\n";
$subject = "Corporate RMAA request form - ".$txtTicketNum;
$body=$formData;

echo "To: ".$to.$newLine;
echo "From: ".$headers.$newLine;
echo "Subject: ".$subject.$newLine.$newLine;
echo "Body: ".$newLine.$body.$newLine;

if (mail($to, $subject, $body, $headers)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }


}//function end


//Send the completed form
sendForm();
?>


#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 August 2006 - 08:18 PM

Did you check if your anonymous access was off?

"Directory Security" tab in website properties in IIS control panel

It should work- I use it regularly for controlling access. and getting email address from active directory


PS And make sure Intgrated Windows authenrication is ON
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 08:33 PM

I will check with network admin about the settings mentioned.

Can i get the username from environment variables instead ?
They are set locally and may be ideal for what i'm trying to do.

Thanks

Did you check if your anonymous access was off?

"Directory Security" tab in website properties in IIS control panel

It should work- I use it regularly for controlling access. and getting email address from active directory


PS And make sure Intgrated Windows authenrication is ON



#18 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 August 2006 - 08:46 PM

Haven't tried ENV variables. I can have a try but not till next Monday when I'll be back in the office and on the network.

There are $_ENV['USERDOMAIN'] and $_ENV['USERNAME'] but I'm currently on my own PC running server and client on 1 PC so I don't know what values will be on a network server.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#19 mikeweb

mikeweb
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 23 August 2006 - 08:54 PM

Those were the first commands i tried on this journey but the page kept returning blank as i was accessing the server's env vars. Surely there's got to be a way of doing this aside from having my users log onto the web page with their long user ids. ultimately that would help me setup the proper email "sent from" when using mail().

Thanks for all your efforts. I've learnt alot from this site and all of you. :-)

Haven't tried ENV variables. I can have a try but not till next Monday when I'll be back in the office and on the network.

There are $_ENV['USERDOMAIN'] and $_ENV['USERNAME'] but I'm currently on my own PC running server and client on 1 PC so I don't know what values will be on a network server.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users