Home
 

split() function divides a string into various elements

Syntax is: array split (string pattern, string string [, int limit])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$ip = "123.456.789.000";
$arr = split ("\.", $ip);   

print "$arr[0] <br>";
print "$arr[1] <br>";
print "$arr[2] <br>";
print "$arr[3] <br>";
?>

Output:
123
456
789
000

Email validation with Regular Expressions

This is also case-insensitive, so it will treat all characters as lower case. It is a really easy way to check the syntax and format of an email address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html>
<body>
<?php
if (isset($_POST['posted'])) {
   $email = $_POST['email'];
   $theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email, $trashed);   //"\w+@\w+.\w+$"
   if ($theresults) {
      $isamatch = "Valid";
   } else {
      $isamatch = "Invalid";
   }
   echo "Email address validation says $email is " . $isamatch;
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="posted" value="true">
Enter your email address for validation:
<input type="text" name="email" value="name@example.com">
<input type="submit" value="Validate">
</form>
</body>
</html>

Log-In Page using Database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<html>
 <head>
  <title>Log-In Page</title>
 </head>
 <body>
 Please enter your user details to log-in here...
 <form action = "login.php" method = "post">
 Username:<br>
 <input type = "text" name = "username">
 <br><br>
 Password:<br>
 <input type = "text" name = "password">
 <br><br>
 <input type = "submit" value = "Log In">
 </form>
 </body>
</html>

login.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$self =     $_SERVER['PHP_SELF'];
$referer =  $_SERVER['HTTP_REFERER'];

if( ( !$username ) or ( !$password ) ){
    header( "Location:$referer" ); exit();
}

$conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Could not connect" );

$rs = @mysql_select_db( "my_database", $conn ) or die( "Could not select database" );
$sql = "select * from users where user_name=\"$username\" and password = password( \"$password\" )";
$rs = mysql_query( $sql, $conn ) or die( "Could not execute query" );
$num = mysql_numrows( $rs );

if( $num != 0 ){
  $msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else{
  header( "Location:$referer" ); exit();
}
?>

<html>
  <head>
     <title>Log-In Authenticated</title>
  </head>
  <body>
   <?php echo( $msg ); ?>
  </body>
</html>

A Simple Login Page using Session

This page aims to show a simple ‘log in’ or ‘sign in’ script written in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
session_start();
if (isset($_POST["submit"])) {
  if ($_POST["user"] == "nilanjan" && $_POST["pass"] == "banerjee") {
    $_SESSION["usernm"] = $_POST["user"];
  }
}
?>

<html>
<head>
<title>User Authentication</title>
</head>
<body>
<?php
if (isset($_SESSION["usernm"])) {
  echo("You are logged in!");
} else {
?>
<form method="post">
<input type="text" name="user" /><br />
<input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Login" />
</form>
<?php
}
?>
</body>
</html>

Iterating Through Object Properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
    class Person {
            public $FirstName = "Sanat";
            public $MiddleName = "Kumar";
            public $LastName = "Roy";
            private $Password = "asdfasdf";
            public $Age = 29;
            public $HomeTown = "Kolkata";
            public $FavouriteColor = "Blue";
    }

    $bill = new Person( );

    foreach($bill as $var => $value) {
            echo "$var is $value\n"."<br>";
    }
?>

Output :
FirstName is Sanat
MiddleName is Kumar
LastName is Roy
Age is 29
HomeTown is Kolkata
FavouriteColor is Blue

Modifying an array with foreach()

Foreach statement to read an array. But at some point, I want to change the value of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
$lunch = array('A' => 1,
               'B' => 2.85,
               'C' => 3.20,
               'D' => 4.80);

foreach ($lunch as $dish => $price) {
    $lunch[$dish] = $lunch[$dish] * 2;
}
echo "The new price of"."<br>";
foreach ($lunch as $dish => $price) {
    printf("%s is \$%.2f.\n",$dish,$price);
	echo "<br>";
}
?>

Output :
The new price of
A is $2.00.
B is $5.70.
C is $6.40.
D is $9.60.

 
 
 

Categories

Tag