Archive for December, 2008

How to get currently logged on windows username in PHP and Javascript

It is very simple to get the current windows username in PHP. The following one line of code will output the username of the system where the server is running. If you are running this from localhost then your system login name will be shown. But you can not get the visitor’s system login username.

<?php echo getenv("username"); ?>

By using javascript (actually VBscript) you can get the visitor’s windows username. But there are also limitations. It only works on IE.

<script language="VBscript">
Dim X
set X = createobject("WSCRIPT.Network")
dim U
U=x.UserName
MsgBox "username: " & U
</script>
<script language="Javascript">
var a = U;
alert("Hello, " + a.toString());
</script>

This code will show you the current windows username. But won’t work if run from http://. Open the page from your computer with IE and it’ll work, otherwise not.

Actually it is not possible to get the windows username of your website visitors as it is a security issue. So don’t waste your time if you are trying to do that.

Simplify your query easily

It’s always boring and time consuming writing long INSERT/UPDATE query. Generally when you need to insert a form data with a large number of fields into a single table it does not make any good sense to write the full query manually. For example “INSERT into tbl set name = ‘$_POST[name]‘ , email = ‘$_POST[email]‘ , ….. may be 30 fields! So how can you minimize your effort?

I have written a very simple PHP function for this:

<?php
function genquery($table, $data, $mode = 'insert into', $condition = '', $raw = '') {
	$res = mysql_query("select * from $table limit 1");

	$field_arr = array();
	for($i=0; $i < mysql_num_fields($res); $i++) {
		$field_arr[] = mysql_field_name($res, $i);
	}
	mysql_free_result($res);

	$qstr = $mode." ".$table." set ";
	$arr = array();
	foreach($data as $k => $v) {
		if(!in_array($k, $field_arr)) continue;
		$arr[] = $k." = '".$v."'";
	}
	$qstr .= implode(', ', $arr);

	$qstr .= ' ' . $raw;

	if($mode == 'update' && $condition != ''){
		$qstr .= ' where ' . $condition;
	}
	return $qstr;
}
?>

You must follow a simple way for using this function. The name of the input fields in the form should be same as the fields in the table of your database. I assume your form will be submitted in POST format. In fact a form with many fields is always submitted in post method.

Here is how to use this function:

<?php
include("qfn.php");
if(isset($_POST['submit'])){  // if the form is submitted

$q = genquery("tableName", $_POST, "INSERT INTO", '', '');
mysql_query($q);
}
?>

For updating use:

$q = genquery("tableName", $_POST, "UPDATE", " id = '$_SESSION[id]' ", '');
// the 4th parameter is the condition for update. Use your own condition.

You can use more than one condition like:

$q = genquery("tableName", $_POST, "UPDATE", " id = '$_SESSION[id]' AND uid = '$_SESSION[uid]' ", '');

The 5th parameter is for raw values like

"entry_date = CURDATE(), status = '1' "

You can use the 5th parameter both for INSERT and UPDATE query. But the 4th parameter is only for UPDATE query.

Hope this will help you a lot and save your time & energy.

Download the php file in zip format.

jQuery Manual in .chm format

jQuery Logo
The online version of jQuery documentation is really great. But they do not provide any offline version of their docs. I’ve got the jQuery docs in a single .chm file. Hope this will help you a lot.

Download jQuery Manual (.chm)

My Elance Master Card from Payoneer!

My Elance Payoneer Master Card

Today I’ve got my Elance master card from payoneer. I’m so glad to have it! You can get Payoneer card from:

  1. Elance
  2. Plimus
  3. oDesk
  4. 2Checkout
  5. MediaWhiz
  6. SFI
  7. iStockPhoto
  8. GetaFreelancer
  9. Incentreward
  10. Copeac
  11. ScriptLance
  12. MetaCafe

and more other sites. You do not need to pay to get the card for the first time. All you have to do is just request for the card from your valid account of any of the websites above and create an account with Payoneer with your account reference. For example: I applied for the card from my Elance account adnanphp.elance.com and then I was taken to the Payoneer website which was “branded” with the logo of Elance. There I created an account using my email address. You can not just create an account directly on Payoneer website to get the card. You must have a reference account. Payoneer is not like paypal, it’s not independent, it is dependent of its affiliate sites. That’s why you must have account with any of their affiliate websites first.

Some sites may need to have some money in your account before applying for the card. The card will be mailed to you from Payoneer. After you receive the card you have to activate it from payoneer website. Then you can load money from the website you have account to withdraw funds. It’s really a great service where PayPal is not available.

The weird experience I had with payoneer is with mailing address format. You can not use any special character for your address field. Like “Road No 9/A” is not accepted, you can not use the / (slash). However I asked help for this and I sent my mailing address to them via email. After one month when I got the card(today) in my address “Road 7/1, House 6/A” is replaced with “Road 7 1, House 6 A” !!! Lucky that I got it at last. So be careful about your address format. You may not be lucky always!