Archive

Archive for the ‘Php’ Category

Receive and manipulate array in form post data

January 7th, 2010 No comments

Arrays can be passed from a html web form to a php script. The ‘name’ attribute of the input type should be specified as an array.

example :

<form method="post" action="arrayformdata.php">
<label>Tags</label>
<input type="text" name="tags[]"/>
<input type="text" name="tags[]"/>
<input type="text" name="tags[]"/>
<input type="text" name="tags[]"/>
<input type="text" name="tags[]"/>
<input type="submit" value="submit">
</form>
</html>
To receive the array in a php script, we use the $_POST as
 
<pre lang="php">
<?php
 
$postedtags = $_POST['tags'];
foreach ($postedtags as $tag){
    echo "<br />$tag";
}
?>
Categories: Php Tags:

PHP localtime – Get current datetime as an array

January 7th, 2010 No comments

The localtime function is used to get the current date and time from the operating system as a C style associative array or list.

Syntax:

array localtime ()
array localtime (integer $Time)
array localtime (integer $Time, boolean $Associative)

Read more…

Categories: Php Tags:

Removing a directory in php with rmdir()

January 7th, 2010 No comments

The php function rmdir() enables developers to remove a directory from the file system. The process trying to delete the directory must have enough permissions and the directory must be empty. The rmdir() function requires a string representing the path to the directory to be deleted. It returns TRUE on success or FALSE on failure.

Example code:

rmdir( "mydir" );
<?php
if (!is_dir('exampledir')) {
    mkdir('exampledir');
}
 
rmdir('exampledir');
?>
Categories: Php Tags:

Getting PHP & Form variable in different operating system

January 7th, 2010 No comments

Why Windows code does not working in Linux and Unix or Linux code does not working in Windows and Unix.

This is the most frequently asked question in PHP. While running a PHP document in Windows we get the form variable by either GET or POST method. For which we use the following code:

$name=$_POST["txtName"]; or $name=$_GET["txtName"];

While using the same code in Linux and Unix you will get a error message. To solve this problem or to run a PHP page in a different operating system without any errors, first declare the GET variables and POST variables globally using the following code. Read more…

Categories: Php Tags:

How to include a file in PHP

January 7th, 2010 No comments

If you have ever wondered how to include another PHP file into a PHP script, this simple one line code sample will show you how.


<?php

// Place this into your PHP page

// and edit the file name

include "some_other_file.php";

?>

Categories: Php Tags: ,

How can I check if a variable is defined?

January 1st, 2010 No comments

if (isset($var)) {
echo “the var is set, and equals $var. It may be empty though.”;
}

http://www.php.net/isset

isset() will tell you is a varaible has been defined. This can bite some
users, because it will return true even if the varable has an empty
value. Look at empty() as well. Sometimes you’ll want to use one rather
than the other. Note that even “0″ and 0 are considered empty. Read more…

Categories: Know more, Php Tags:

How can i do auto redirect using php?

January 1st, 2010 1 comment

Put the following code at the top of your page.

<?
$URL="http://www.yourdomain.com";
header ("Location: $URL");
?>
Categories: Php Tags: ,

PHP Date Formats

December 29th, 2009 No comments
The following characters are recognized in the
format
parameter string
format
character
Description Example returned values
Day
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l (lowercase ‘L’) A full textual representation of the day of the week Sunday through Saturday
N ISO-8601 numeric representation of the day of the week (added in
PHP 5.1.0)
1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or
th. Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365 Read more…
Categories: Php Tags: ,

Show users IP in a graphic – PHP Script

December 19th, 2009 No comments

Show IP Display visitor’s IP Address in a graphic with this simple php code.
(Please do not link to this image, it is for demonstration only.)
Your IP Address

create a new page, copy the text below and save it as image.php (or whatever you wish)

<?php
$img_number = imagecreate(275,25);
$backcolor = imagecolorallocate($img_number,102,102,153);
$textcolor = imagecolorallocate($img_number,255,255,255);<span id="more-1257"></span>
 
imagefill($img_number,0,0,$backcolor);
$number = " Your IP is $_SERVER[REMOTE_ADDR]";
 
Imagestring($img_number,10,5,5,$number,$textcolor);
 
header("Content-type: image/jpeg");
imagejpeg($img_number);
?>

All you’ll need to display on other pages is the code below.

<img src="http://yoursitehere.com/image.php" border="1" alt="" />
Categories: Php Tags:

PHP DateTime and DateTimeZone Tutorial

November 22nd, 2009 3 comments

With each new version PHP is getting more and more object oriented. In version 5.x we get two useful classes for date and time handling. Many programmers are still using outdated methods which are available in PHP mainly for compatibility reasons, so i want to introduce you to DateTime and DateTimeZone objects.
Server default Timezone

Before i do that, first let’s start with some basics, which tend to cause a lot of troubles in the beginning. Each computer has it’s own default timezone and local time set. If you are using Microsoft Windows, then probably timezone on your local machine is already set correctly, on the other hand if you are using Linux chances are that you will have to do it manually (i had to, however i am not using typical Linux so maybe this is just me). Read more…

Categories: Php Tags: