Archive

Archive for April, 2009

mail() in PHP

April 30th, 2009 kumarasamy No comments
PHP allows you to send a mail from script. PHP has wide range of options in mail() function to use it.
To send a mail(text) from script
			<?php
				$to = "user@domain.com";
				$subject = "My First Mail Script Demo from PHP";
				$message = "Hello! This is a simple email message i tried from PHP.";
				$from = "user2@domain.com";
				$headers = "From: $from";
				mail($to,$subject,$message,$headers);
				echo "Mail Sent.";
			?>
		
The above code just sends a plain text to the mail specified as sender. In-order to send the mail with text/html, you can more parameters to the header, so that it”ll send’’s an HTML mail. The following two lines tells our PHP script to send an HTML mail message instead of plain text mail. And let us add some more parameters to header like CC and Bcc, which add the CC and Bcc to our mail.
		<?php
			// To send the HTML mail we need to set the Content-type header.
			$headers = "MIME-Version: 1.0rn";
			$headers .= "Content-type: text/html; charset=iso-8859-1rn";
			$headers  .= "From: $fromrn";//options to send to CC and Bcc
			$headers .= "Cc: cc@domain.com";
			$headers .= "Bcc: Bcc@domain.com";
		?>
By using the header we can send the attachment also.
In-addition to this, in PHP we have IMAP library to check the mails from your POP/IMAP server(s). The dll file php_imap.dll has the support for the IMAP function in PHP. This dll will be installed in ext folder under the PHP is in configured. This library can also be used to open streams to POP3 and NNTP servers, but some functions in this library and features are only available on IMAP servers. The following code will check the mail in your POP server’’s Inbox folder and display the
		<?php
			//Open the POP3 Server's using the username and passowrd
			$mbox = imap_open("{pop.server.com:110/pop3}INBOX", "username", "password");
			$MC=imap_check($mbox); //Check the Mailbox using mbox handle
			$MN=$MC->Nmsgs;
			//Fetch mbox''s message''s overview into an Array
			$overview=imap_fetch_overview($mbox,"1:$MN",0);
			$size=sizeof($overview);
			for($i=$size-1;$i>=0;$i--){
				$val=$overview[$i];
				$msg=$val->msgno; //Message #
				$from=$val->from; //From address
				$date=$val->date; //Date Message sent
				$subj=$val->subject; //Subject line of the mail
				echo "<tr><td align=''center''>$msg</td><td>$from</td><td>$date</td><td>$subj</td></tr>";
			}
			//Close the handle
			imap_close($mbox);
		?>	
Apart from the aboce taken parameters from the we can add many parameters into the overview handler. To check the above script in action click here
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

SQL Injection

April 29th, 2009 kumarasamy No comments
SQL injection vulnerabilities and how to overcome.According to wiki SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.Let me give you an example of one of a very basic vulnerability.Incorrectly filtered escape charactersThis form of SQL injection occurs when user input is not filtered for escape characters and is then passed into a SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application.The following line of code illustrates this vulnerability
			statement = “SELECT * FROM users WHERE name = ‘” + userName + “‘;”
		
This SQL code is designed to pull up the records of the specified username from its table of users.However, if the “userName” variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the “userName” variable as a’ or ‘t’=”t renders this SQL statement by the parent language
			SELECT * FROM users WHERE name = ‘a’ OR ‘t’=''t’;
		
If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of ‘t’=”t’ is always true.While most SQL Server implementations allow multiple statements to be executed with one call, some SQL APIs such as php’s mysql_query do not allow this for security reasons.This prevents hackers from injecting entirely separate queries, but doesn’t stop them from modifying queries.The following value of “userName” in the statement below would cause the deletion of the “users” table as well as the selection of all data from the “data” table (in essence revealing the information of every user)
			a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '%'
		
This input renders the final SQL statement as follows
			SELECT * FROM users WHERE name = ‘a’;DROP TABLE users; SELECT * FROM DATA WHERE name LIKE ‘%’;
		
SQL injection is a nasty thing. An SQL injection is a security exploit that allows a hacker to dive into your database using a vulnerability in your code. Since many PHP programs use MySQL databases with PHP, so knowing what to avoid is handy if you want to write secure code.Here are some SQL injection cheat sheet that has a section on vulnerabilities with PHP and MySQL. If you can avoid the practices the cheat sheet identifies, your code will be much less prone to scripting attacks.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Categories: General Tags: ,

XHR – Backbone for Web2.0

April 22nd, 2009 kumarasamy No comments
XHR-Xml HTTP Request.XMLHttpRequest is a JavaScript object that was designed by Microsoft, adopted by Mozilla, and is now being standardized in the W3C. It provides an easy way to retrieve data at a URL. Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports connections other than HTTP. The most interesting story about XHR is, MS has introduced in 2000 for it’’s Web Outlook.It has been available since the introduction of Internet Explorer 5.0 and is accessible via JScript, VBScript and other scripting languages supported by IE browsers.
			var req = new XMLHttpRequest();
		
The above javascript creates an XHR, whcih can be used to request a data from Server asynchronously or synchronously.The following method shows how to create an XHR in different versions of browser(s).
			var objectXHR = "";
			try {
				objectXHR = new XMLHttpRequest(); //Supports in Latest Browsers
			}
			catch (trymicrosoft) {
				try {
					objectXHR = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (othermicrosoft) {
					try {
						objectXHR = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (failed) {
						objectXHR = false;
					}
				}
			}
			
The objectXHR can be used to send the request to server asynchronously or synchronously. The following code will send the synchronously.
			var XHR = new XMLHttpRequest();
			XHR.open(''GET'', ''http://www.techmaddy.com/'', false);
			XHR.send(null);
				if(XHR.status == 200)
					alert("Yeahhh....My First Successful Sync Request ");
		
The Open method used to send the request to servers. The third parameter decides the request shoud be asynchronous or synchronous.
			XHR.open(''GET'', ''http://www.mozilla.org/'', false);
		
The following code calls the server in Asynchronous mode
			var XHR = new XMLHttpXHRuest();
			XHR.open(''GET'', ''http://www.techmaddy.com/'', true);
			XHR.onreadystatechange = function () {
				if (XHR.readyState == 4) { //transaction Complete
					if(XHR.status == 200) //Successful Return
						dump(XHR.responseText);
					else
						dump("Error loading pagen");
				}
				else {//Show the busy icon	}
			};
			XHR.send(null);
		
The XHR is most useful in refreshing the page partially. Validating the username, password strength check.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash playerPlugin by wpburn.com wordpress themes