GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, January 2, 2011

PHP String Function

To get the length of the string, type:
$lol = "lady gaga";
echo strlen($lol);
output>> 9

To get part of the string,
echo substr("Holla World",6);    // substr(text, start)
output>> World
echo substr("Holla World",6,2);    // substr(text, start, length)
output>> Wo

To get the position of the specified string in a text, type:
echo strpos("Holla World","Wo"); // strpos(text,searched,[start])
output>> 6
echo strpos("Holla World","o",1);
output>> 2
        
To break a string into array, type:
print_r(explode(" ","Holla World"));    // explode(separator, 
                                        // string);
output>> Array ( [0] => Holla [1] => World ) 
               

Share/Bookmark

Date In PHP

To display a date, use date() function:
echo date("l, F d, Y");    // day - month - date - year
ouput>> Sunday, January 02, 2011

echo date("h:i:s");    // hours:minutes:seconds
output>> 11:36:07
          

Share/Bookmark

FileSystem In PHP

To get all content from a file, and then give it to a variable, use:
$contents = file_get_contents("lola.txt");
echo $contents;

To insert a string to a file, use:
$text = "hello world\n This is lady gaga\n This is luna maya";
file_put_contents("lola.txt",$text);    // return char of text
                                        // inserted

To know file size, use:
echo filesize("lola.txt");
       
Share/Bookmark

Reading A File In PHP

To open a file, type:
    $file = fopen("lola.txt","r") or exit("Cannot open the
            file");

To read one line, use:
    echo fgets($file);    // this pointer will advance one 
                          // per line

To check wheter reach end of file, type:
    feof($file);

To read a whole of file line by line, use:
    while(!feof($file)){
        echo fgets($file)."<br/>";
    }

To read a whole of file char by char, use:
    while(!feof($file)){
        echo fgetc($file)."<br/>";
    }
              
Share/Bookmark

Session In PHP

To use session, type
session_register("username");    // username is variable 
                                 // $username="lady gaga"
The you check it by using:
session_start();    // this must the first you type in the file.
$name = $_SESSION['username'];
echo $name;

For complete example, look
checklogin.php
<?php

    $username = $_POST['username'];
    $password = $_POST['password'];

    mysql_connect("localhost","root","");
    mysql_select_db("satu");
    $query = "SELECT * FROM login WHERE username='$username'
              AND password='$password'";
    $result = mysql_query($query);
    $count = mysql_num_rows($result);

    if($count==1){
        session_register('username');
        session_register('password');
        header("Location: hello.php");
    }else{
        header("Location: index.php");
    }
?>


Type this in hello.php
<?php 
    session_start();
    if(!session_is_registered('username')){
        header("location: index.php");
    }
  
    echo "<h1>Hello, $_SESSION['username']<h1>";
?>


To make a logout app, type:
<?php
    session_start();
    session_destroy();
?>

           
Share/Bookmark

Login Application In PHP

In this stage, first you need to create  a table in database to save the records:

Use this command in MySql interactive:
mysql>> CREATE DATABASE data
Query OK, 1 row affected (0.00 sec)
mysql>> CREATE TABLE login
     >> (id INT NOT NULL AUTO_INCREMENT KEY,
     >> username CHAR(20),
     >> password CHAR(20));
Query OK, 0 rows affected (0.06 sec)
mysql>> INSERT INTO login
     >> VALUES
     >> (null, 'lady gaga', 'gaga');
Query OK, 1 row affected (0.02 sec)


After finishing above, try to create php file:
index.php
<form action="checklogin.php" method="post">
    Username:
<input type="text" name="username"/><br/>
    Password:
<input type="password" name="password"/><br/>
   
<input type="submit" value="Log In"/>
</form>




checklogin.php
<?php

    $username = $_POST['username'];
    $password = $_POST['password'];

    mysql_connect("localhost","root","");
    mysql_select_db("satu");
    $query = "SELECT * FROM login WHERE username='$username'
             AND password='$password'";
    $result = mysql_query($query);
    $count = mysql_num_rows($result);

    if($count==1){
        header("Location: hello.php");
    }else{
        header("Location: index.php");
    }
?> 


hello.php
<?php
    echo "<h1>Hello You are in login state<h1>";
?>
Share/Bookmark

Extending Array Object

Below is a sample on how to extends an array object.

Array.prototype.getMinValue = function(){
    var minIndex = 0;
    for(i=1;i<this.length;i++){
        if(this[i]<this[minIndex]){
            minIndex = i;
        }
    }
    return this[minIndex]
}

You can call the above method by:
var data = [5,4,8,3,1,6];
alert(data.getMinValue());
output>> 1
       

Share/Bookmark