Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, February 18, 2013

PHP example - Google Recaptcha validation using jquery (AJAX)

In earlier post we have seen JSP example, how to validate Recaptcha using jquery,  Now we will see how to achieve the same in PHP.

To know more about Google recaptcha, click here.

To run php example add recaptcha-php-1.11 in your application. Please see the self explanatory PHP code below.

test.php

Tuesday, January 22, 2013

Load PROPERTIES file in PHP like JAVA


A property file is basically consist of a data in key/value pair. In PHP is bit different than java. In java we used to create a .properties file and load using below code

Properties prop = new Properties();
prop.load(new FileInputStream("sample.properties"));

Now we will see equivalent code in PHP. In PHP instead of .properties it's .ini file and it is loaded using parse_ini_file() method. Please see the self explanatory code below

myProperties.ini

; This is a property file
; Comments start with ';', as in php.ini

id = 001
name = Alex
state = NY

loadProperties.php


<?php
$ini_array = parse_ini_file("myProperties.ini");
echo "Printing the array<br/>";
print_r($ini_array);

echo "<br/>";
echo "Get values by name";
echo "<br/>id = ".$ini_array['id'];
echo "<br/>name = ".$ini_array['name'];
echo "<br/>state = ".$ini_array['state'];
?>

PHP - read and write text file


Below are the sample codes to read and write text file using PHP

Read content of a file

<?php
//file name
$filename = "textFile.txt";

//open the file with Read only Mode. Starts at the beginning of the file.
$file = fopen($filename, "r");

//all content the file
$contents = fread($file, filesize($filename));

//close the file
fclose($file);

//print the content
echo $contents;
?>


Read content of a file line by line

<?php
//file name
$filename = "textFile.txt";

//open the file with Read only Mode. Starts at the beginning of the file.
$file = fopen($filename, "r");

//loop the file until the end is reached
while(!feof($file))
{
echo fgets($file)."<br>";
}

//close the file
fclose($file);
?>


Monday, January 21, 2013

500 Internal Server Error / hide .php extension using .htaccess


In my first php project I was required to hide .php extension from the URL. For example if the URL is 'http://localhost/Test/request.jsp' it should display 'http://localhost/Test/request' in browser. I found many links which were showing how to achieve that using .htaccess file however it was not clear where to place that file, under root directory OR under the website directory. I tried both the locations but as soon as i add anything in the file .htaccess it used to give below error.

Error:



Friday, January 11, 2013

Javascript - Unterminated string constant


If you receive below error, check at the specified line. As the error suggest, most probably you have added a line break or some incorrect syntax. To resolve the error just remove the line break or check for proper syntax and the error would go off.

Message: Unterminated string constant
Line: 443

 If you are using Linux, check for line break using below command in vi editor. It will show you symbol '$' at the end of each line. Just remove the line break at the specified line.

 :se list

As the error suggest 'Unterminated string constant', mostly it may occur due to some incorrect syntax.

Tuesday, January 8, 2013

JavaScript - How to get random numbers and random alpha-numeric numbers?


In this article we will see how to generate random numbers and alpha-numeric numbers in JavaScript  We will use JavaScript Math.random function to generate random numbers.

Math.random function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Please see the below function.


1. Get random numbers

//get random numbers
function getRandomNumber() {
var max = 10;
var randomnumber = Math.floor(Math.random()*max)
return randomnumber;
}

2. Get random numbers between minimum and maximum range

//get random numbers between minimum and maximum range
function getRandomNumber() {
var min = 10;
var max = 20;
var randomnumber = Math.floor((Math.random()*(max - min + 1))+min);
return randomnumber;
}


Thursday, October 4, 2012

Why PreparedStatement is used to prevents SQL Injection?

What is a SQL Injection?

In simple language SQL injection is injecting malicious sql code into the application's sql that may help attacker to retrieve sensitive information like user name / passwords etc OR it can also be used by hackers for login without authentication.

For example, We have a human resource management system, where in employee logs in and can view his / her sensitive information like attendance, salary etc. To log on to system every employee requires username and password. Now suppose below function is used to do authentication.

private boolean isAuthenticate(String userName, String password) {

Statement stat = connection.createStatement();

String query = "SELECT 1 FROM EMPLOYEE WHERE USER_NAME  = '"+userName+"'  AND PASSWORD = '"+password+"';";

ResultSet rset = stat.executeQuery(query);

if (rset.next()) {
return true;
}
else {
return false;
}
}


if the above query fetches result then allow the user to enter into the system else don't allow. You might think that the above process is correct but it's not like that, the above function has a serious flaw. let's see how.


Saturday, August 18, 2012

PHP - Handling File Uploads

In this article we will see how to upload files on PHP web page. You have to set up the form as a multipart form if you want to use file upload controls. In this example, that means you set the <form> element's enctype (encoding type) attribute to "multipart/form-data". You can also set the action attribute to the URL where you want the file data to be sent and the method to "post".

About the example : We have a file fileUploadForm.php, it will be used as a form to browse and upload file and of course it can have other form elements (text, checkbox, radobutton etc) as well. The action attribute is set to URL submitFile.php which will handle the file. It can specify the location where to save the file. Please see the self explanatory example below.


Thursday, August 16, 2012

PHP - Form elements

In this article we will see how to create form in PHP and send data to next PHP page. Like other languages you need to specify the methods with which your data will be sent - "GET" and "POST". If you've used the POST method, you can find that data in the $_POST array similarly if you've used the GET method, you use the $_GET array. These array are "super global" arrays, which means that they're available to you without having to use the global keyword. Also, there is a $_REQUEST array which holds data from both $_GET and $_POST array.

We will create a form which contain various form elements like text box, password, radio button, select dropdown, text area and check box.

Saturday, August 11, 2012

PHP - Database connectivity


Below are the 6 steps used for PHP database connectivity.


Step 1. Create a connection to the database using mysql_connect() function, which returns a connection object.

Syntax : $con = mysql_connect("localhost","root","password");

Step 2. Select database or schema using mysql_select_db() function

Syntax : mysql_select_db("database_name", $con);

Step 3. Execute the query using mysql_query() function. This function is used to send a query or command to a MySQL connection. It returns result object

Syntax : $result = mysql_query($sql);

Step 4. Looping through results. mysql_fetch_array() function is used to fetch a result row as an associative array, a numeric array, or both.

Syntax : $row = mysql_fetch_array($result)

Step 5. Printing the results. Simply printing the values from result array.

Syntax : echo $row['column1']." ".$row['column2'];

Step 6. Closing the connecting using mysql_close() function.

Syntax : mysql_close($con);


Please see the complete tested code, Save the page with .(dot) php extension e.g. ConnectDatabase.php. Provide correct credentials and see the output.