Programmer

PHP Form Validation

<?php
// define variables and set to empty values
$nameError = $emailError= $genderError = $websiteError = “”;
$name = $email = $gender = $comment = $website = “”;

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
if (empty($_POST[“name”])) {
$nameError = “Name is required”;
} else {
$name = test_input($_POST[“name”]);
// check if name only contains letters and whitespace
if (!preg_match(“/^[a-zA-Z ]*$/”,$name)) {
$nameError = “Only letters and white space allowed”;
}
}

if (empty($_POST[“email”])) {
$emailError = “Email is required”;
} else {
$email = test_input($_POST[“email”]);
// check if e-mail address syntax is valid
if (!preg_match(“/([\w\-]+\@[\w\-]+\.[\w\-]+)/”,$email)) {
$emailError = “Invalid email format”;
}
}

if (empty($_POST[“website”])) {
$website = “”;
} else {
$website = test_input($_POST[“website”]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match(“/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”,$website)) {
$websiteError = “Invalid URL”;
}
}

if (empty($_POST[“comment”])) {
$comment = “”;
} else {
$comment = test_input($_POST[“comment”]);
}

if (empty($_POST[“gender”])) {
$genderError = “Gender is required”;
} else {
$gender = test_input($_POST[“gender”]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>PHP Form Validation</h2>
<p><span class=”error”>* required field.</span></p>
<form method=”post” action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>“> 
   Name: <input type=”text” name=”name”>
   <span class=”error”>* <?php echo $nameError;?></span>
   <br><br>
   E-mail: <input type=”text” name=”email”>
   <span class=”error”>* <?php echo $emailError;?></span>
   <br><br>
   Website: <input type=”text” name=”website”>
   <span class=”error”><?php echo $websiteError;?></span>
   <br><br>
   Comment: <textarea name=”comment” rows=”5″ cols=”40″></textarea>
   <br><br>
   Gender:
   <input type=”radio” name=”gender” value=”female”>Female
   <input type=”radio” name=”gender” value=”male”>Male
   <span class=”error”>* <?php echo $genderError;?></span>
   <br><br>
   <input type=”submit” name=”submit” value=”Submit”> 
</form>

<?php
echo “<h2>Your Input:</h2>”;
echo $name;
echo “<br>”;
echo $email;
echo “<br>”;
echo $website;
echo “<br>”;
echo $comment;
echo “<br>”;
echo $gender;
?>


</body>
</html>


 

PHP Form Validation

* required field.

Name:
*
E-mail:
*
Website:

Comment:

Gender:
Female
Male
*


Your Input:

Name
Test@gmail.com
www.example.com
Test Comment
Female

Monday, May 26th, 2014 PHP, phpvalidation, Programmer, Programming No Comments

Simple Capcha using php

Capcha Code:

Save this code as “capcha.php”

[php]

<?php
session_start();
$text = rand(10000,99999);
$_SESSION[“vercode”] = $text;
$height = 25;
$width = 65;

$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;

imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
[/php]

——————————————————————————————————
HTML:

<img src=”capcha.php” alt=”” align=”top” /> <input id=”vercode” class=”cap” name=”vercode” type=”text” align=”texttop” />
<div>

<input class=”sub” name=”subi” type=”submit” value=”Register” />

</div>

 

Must set session to use the capcha

PHP Code for the html page:

[php]
<?php if ($_POST[“vercode”] != $_SESSION[“vercode”])
{
header(“location:contact.php?ver”); //incorrect verification code }
?>
[/php]

Monday, May 19th, 2014 PHP, Programmer, Programming No Comments

PHP Sessions

Starting a PHP Session

  • The session_start() function must appear BEFORE the <html> tag:

[php]

<?php session_start(); ?>

<html>
<body>
</body>
</html>
[/php]

  • The above code is used to register the user session in server.

Destroying a Session

[php]
<?php
session_destroy();
?>
[/php]

  • The above code is used to destroy the complete session from the server.

NOTE :  The session is very very important to save the current user information on the server. Using UID(user id), The server saves the user information on the server.

Wednesday, April 23rd, 2014 PHP, Programmer, Programming No Comments

Logos – Unknown Facts

Logos are not only meant for design.As a end user one can view the logo’s look and feel.But the designers should know the hidden meaning behind it.

View some of them here.

Can you find the symbol which is hidden in the Federal Express logo? Look at the logo. You can see the ‘arrow’ between the E and X in this logo.The arrow was to highlight swiftness and accuracy, which are part of the positioning of the company.

Paul Rand designed the iconic IBM logo in the year 1972.Later on 1981 he designed this ‘eye bee M’ logo. Actually IBM stands for “International Business Machines”.

An amazing instance of symmetry and order is the SUN Microsystems logo. When you observe the logo, the letters u and n while arranged adjacent to each other look a lot like the letter S in a perpendicular direction.

Have a look at the arrow in the logo. You may imagine that the arrow does nothing here. But it convey a message that amazon.com has everything from a to z and it also represents the smile brought to the customer’s face.

Tags: , , , ,

Tuesday, April 28th, 2009 Programmer No Comments

Programmers Reaction

See the reactions of an ordinary man and a programmer while in danger.

Ordinary Man

Programmer

Tags: ,

Thursday, April 9th, 2009 Programmer No Comments

Definition of Open Source

The term “Open source” does not just represent right to use the source code. The distribution terms of open-source software has a necessity to meet the terms with the subsequent standards:

  • Redeployment without charge
  • The certificate does not limit anyone from advertising or giving away the software as a module of a combined software distribution encloses programs from numerous different sources. The license does not entail a fee or other payment for such sale.

  • Source Code
  • The program should consist of source code. It has to permit sharing of source code in compiled form. If some form of a product is not circulated with source code, there must be a fine revealed means of how to obtain the source code for a rational reproduction cost preferably, downloading via the Internet without charge. The format of source code must be like a programmer can be able to alter the program. Intermediate forms like output of a preprocessor or translator are not permitted.

  • Derived Works
  • The warrant must let them to make any modifications and derived works. It should permit them to be distributed the software under the similar conditions as the license of the original software.

  • Reliability of the Author’s Source Code
  • The license possibly will confine source-code from being distributed in customized form only if the license agrees to distribute “patch files” with the source code for the intention of transforming the program at build time. The license must openly allow distribution of software built from modified source code. The license may need derived works to bring out different name or version number from the original software.

  • No inequity against Persons or Groups
  • The license is required to distinguish beside any person or group of persons.

  • No favoritism against Fields of Endeavor
  • The license should not put a ceiling on making use of the program in a particular field of endeavor. For instance, it may not confine the program from being used in a trade, or from being used for genetic observations.

  • Distribution of License
  • The rights enclosed to the program should be relevant to all to whom the program is rearranged with no need for implementation of an additional license by those parties.

  • License should Not Be precise to a Product
  • The rights attached to the program should be unique. It should not depend on other program’s which is part of a particular software distribution.

  • License Must Not Restrict Other Software
  • The authorization should not put limitations on other software that is distributed in conjunction with the licensed software. For instance, the license should not claim that all other programs distributed on the similar medium ought to be open-source software.

  • License Must Be Technology-Neutral
  • No stipulation of the license may be predicated on any individual technology or style of interface.

Tags: ,

Friday, February 20th, 2009 Programmer No Comments

Are you a real programmer? How can you tell that?

This is really amusing for one and all to read.

Machine code is your pseudo code

You trust that by means of a linker or a macro library to make a program is “dirty”, a real programmer does not get baffled when the source code get to 6000 lines in one file

You suppose that PC playoffs are a squander of CPU cycles.

While you are at a monetary firm coding for mainframes in mainframe assembly (the manager inform him to code in Assembly rather than machine code so the other ‘normal’ programmers may possibly be aware of what is going on)

You construct your individual programming languages and provide them crappy names.

You were not being fond of multicore systems. You accept as true that if you cannot formulate a program to run fast enough on a single core, you are not a programmer.

You will never utilize operating system interrupts … you make your own.

If your random number generator creates the random numbers from zero to infinity in 5 seconds, you believe it as very slow.

You consider that remarking the source code is for the amateur.

You don’t make use of computers outside of work. You will not have belief on other programmer’s code.

You judge that technologies like PHP, Python or VBA were not a programming language- it is only scripting language.

You don’t want to use Object Orientation for the reason that it makes your work trouble-free. In any case you wish for a challenge.

C++ and APL programming languages was a toy to you.

Someone point out the languages like VB and JAVA to you, you will give a punch them on the head.

Even if your code has been typed up, it was already optimized.

You get clear of the Back Space key on the Keyboard as you do not make any mistakes.

You don’t like to have any book or notepads; it’s all in your head.

Your programs are not user friendly, even if they are bug free.

You have a nightmare of creating your own CPU for yourself one day and maintain it in your parent’s garage.

You thought that IDE’s, Programmer Tools and Syntax Highlighters are for people who do not be familiar with what they are doing.

You return back the computer mouse to the technician, with a screw driver stabbed through it. Real programmers will make use of command prompt only.

If the client does not be fond of what you perform for them, you think that the client is an idiot and you say “No” to alter anything.

Who wants friends when you can program your own friends.

Tags: , ,

Tuesday, October 21st, 2008 Programmer No Comments

How to handle errors using Boolean flags

This error handling method may be the most ancient. Boolean flags were used by many programming languages, and PHP will not be an exception. Even though they are very simple to apply, the main negative aspect is that they were not very informative about the mistake that happened and its perspective. Here is an unsophisticated implementation of the FileReader class, which make use of Boolean flags:

[php]
class FileReader{
var $file;
var $fileDir=’fileDir/’;
function FileReader($file){
if(!@file_exists("{$this->fileDir}{$file}.php")){
return false;
}
$this->file=$file;
}
function getContent(){
if(!@$content=file_get_contents("{$this->fileDir}{$this->file}.php")){
return false;
}
return $content;
}
}
[/php]

Bearing in mind about the explanation for the above example, class errors might be handled as follows:

[php]
$fr=new FileReader(‘inexistent_file’);
if(!$fr->getContent()){
die(‘Unable to read file contents’);
}
else{
echo $fr->getContent();
}
[/php]

In the example, @ error suppression operator is intentionally used, so as to stay away from the complaints of the PHP interpreter and return a false value (or 0 or -1) when a failure happens. At first look, you can observe the incompetence of this method, plus its restricted flexibility. On the other hand, this approach has established to be quite victorious in procedural applications, or when client code is able to handle straightforward errors without corrupting the entire application.

Nowadays, we have investigated the pros and cons of common error handling approaches in PHP 4. Certainly, in huge web applications, a set of error controlling classes is preferred, so you are able to handle errors through a centralized point. Conversely, the trigger_error () / set_error_handler () combination may suit the requirements of small projects, so it is worth considering.

Tags: ,

Thursday, October 16th, 2008 Programmer, Programming, Troubleshoot No Comments

Guidelines to handle Db connection

Please confirm that you close the original connection to a database all the time and that you are using connection pooling to get better processing speed of database connection creation etc. Here are some tips for you:

1. Use web.config to store up the connection string:

2. Use this connection string forever and do not make any changes to it. If you were not doing this (for example using different DSNs) you will disable the connection pooling feature:
string dsn = System.Configuration.ConfigurationSettings.AppSettings[“dsn”];

3. Do not perform any tricks to try to deal with connections etc, just proceed and use it (no wrapper classes or so involved):
SqlConnection conn = new SqlConnection(dsn);

4. Formulate the time between conn.Open() and conn.Close() as small as possible. E.g. don’t do this:
SqlConnection conn = new SqlConnection(dsn);
Conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(…);

//and further cmd parameters actions and so on

SqlDataReader reader = cmd.ExecuteReader();

//use the reader and do a bundle of additional tasks, not relying on the database

conn.Close();

But rather, use:
SqlConnection conn = new SqlConnection(dsn);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(…);

//and more cmd parameters actions etc

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

//use the reader
conn.Close();
//perform a batch of other work, not relying on the database

5. Always close the connection when you are done, even when an exception occurs. To do this, use the try…finally pattern:
SqlConnection conn = new SqlConnection(dsn);
try
{
//some init depending on conn
conn.Open();
//minimum number of lines of code depending on the open connection
}
finally
{
if (conn.ConnectionState == ConnectionState.Open)
conn.Close();
}
or the using pattern in C#:
using (SqlConnection conn = new SqlConnection(dsn))
{
//…
conn.Open();
}
This will close the db connection repeatedly while leave from the using block.

Tags:

Friday, October 10th, 2008 Programmer No Comments

Local Host in Mozilla or Netscape browsers

Most of the users who are using local host in IIS are known to work only in Internet Explorer (IE) browser.

However, you may also browse your Localhost in both the Mozilla and in Netscape Browsers also. To browse your localhost in Mozilla and Netscape browsers just follow the simple steps provided below:

1) Type about:config in your address bar
2) In Mozilla it will prompt an alert message click the button “I’ll be careful, I promise”
3) Now in the filter box search for “network.automatic-ntlm-auth.trusted-uris
4) Double click on the Preference Name.
5) Enter the value “localhost” and click OK.
6) That’s it.. Your browser is now configured to use localhost.

Hope this is so helpful for many users.

Tags:

Thursday, July 24th, 2008 Programmer No Comments
Request a Free SEO Quote