opens a directory with opendir() in php

open.php :

<?php
//Open send directory
if(@$dir = opendir("send"))
{
//List files in send directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
}
else
{
echo 'directory not opened';
}
?>

output :
 1)
directory not opened

2)
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

Generate random password


/**
* Generate a random character array from the list of $possible characters
*
* @param array $possible Array of possible characters
* @param int $length Length of string to generate
* @return array An array of $length random characters
*/
function randomStringArray($possible, $length) {
$stringArray = array();
$possibleCount = count($possible);

$i = 0;
while ($i < $length) {
// pick a random character from the possible ones
$char = $possible[mt_rand(0, $possibleCount - 1)];

// we don't want this character if it's already in the password
if (!in_array($char, $stringArray)) {
$stringArray[] = $char;
$i++;
}
}

return $stringArray;
}

/**
* Generate password that contains 6 letters and 2 numbers
*/
function generatePassword() {
$letters = array_merge(range('a', 'z'), range('A', 'Z'));
$numbers = range('0', '9');
$chars = array_merge(
randomStringArray($letters, 6), randomStringArray($numbers, 2));
shuffle($chars);
return implode('', $chars);
}

About me and PHP

Hello World

My name is Sco Sban Wart :) It is my blog :) I will write about php security and put some interesting information from the net.

PHP is a very popular language. Every PHP developer and hoster should understand the primary attack vectors being used by attackers against PHP applications.

This article is the underlying research behind the SANS Top 20 2005's PHP section. The methodology used in the preparation of this article is to review all Bugtraq postings containing the word "PHP" and categorize each unique flaw. The author analyzed the most popular flaws / attacks, and researched prevention techniques, resulting in this article.

Form Validator

<script Language="JavaScript">
function FormValidator(theForm){
if (theForm.Name.value==""){
alert("Please enter your name.");
theForm.Name.focus();
return (false);
}
if (theForm.Subject.value==""){
alert("Please enter the subject");
theForm.Subject.focus();
return (false);
}
return (true);
}
</script>

<form method="POST" action="add.php" onsubmit="return FormValidator(this)" name="FormName">
<p>
Name : <input type="text" name="Name" size="20"><br>
Subject : <input type="text" name="Subject" size="20"><br>

<input type="submit" value="Submit" name="Submit"> <input type="reset" value="Reset" name="Reset">
</p>
</form>

file write in php

<?php

if(isset($_POST['name'])&&isset($_POST['age'])&&isset($_POST['fname']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  $fname=$_POST['fname'];
  if(!empty($name)&&!empty($age)&&!empty($fname))
  {
    $hand=fopen($fname.'.txt','w');
    fwrite($hand,$name." ");
    fwrite($hand,$age."\r\n");
    fclose($hand);
  }
 else
    echo 'fill all fields';
}
?>

<form action="file-write.php" method="POST">
name :<input type="text" name="name">
age :<input type="text" name="age">
file name : <input type="text" name="fname">
.txt
<input type="submit" value="submit">
</form>

output :


note :
1) enter all fields
2) the last field is file name
3) first it clears all data from that file and write you entered data into that file
4)if that file not exists then it creates and write that file

Connecting to server, create database and creating table

exercise 1
connect to apache server root and the password, then create a database and to create table.
use this php code

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE alex",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("alex", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>