Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Monday, May 30, 2011

PHP Connect to MySQL

MySQL is the preferred database to use when dealing with PHP. It's an old system that works quite well still and doesn't look to be replaced anytime soon. We are going to connect to a database, select a table, select some data then print the data back out.
<?php
mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
?>
After connecting and selecting a database, select some data from the tables.
<?php
mysql_connect("127.0.0.1","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$query = mysql_query("select * from users");
$get_rows = mysql_fetch_array($query);

print_r($get_rows);
?>
Below is the sample database we used for this post.
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `email` varchar(80) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES
(1, 'admin', 'password', 'admin@unix.com'),
(2, 'codershed', 'admin', 'cs@codershed.us'),
(3, 'cs', 'cs', 'worker2@codershed.us');

PHP Read text files

Reading files is just as easy as writing them.
<?php
$str = file_get_contents("myfile.txt");
echo $str;
?>
People might not like it, but it works and is super easy.

PHP Write to file

Writing to files with PHP is a snap, it's super easy, like most everything about PHP. First, open a file handle (or stream), write to the stream, then close it. ALWAYS CLOSE.
<?php
$fh = fopen("myfile.txt","w");
fwrite($fh,"hello world\n");
fclose($fh);
?>
When you write to the file, try to write only once if you can. If you need to store data, build it up then write it to file, like below.
<?php
$data = array("My data","hello","world","goes","here","write","to","file");
$str = "";
for($i=0;$i<sizeof($data);$i++)
{
    $str .= $data[$i]."\r\n";
}

$fh = fopen("myfile.txt","w");
fwrite($fh,$str);
fclose($fh);
?>

PHP MD5

To MD5 some data is easy simply use the md5() function.
<?php
echo md5("hello");
?>
Store it in a variable
<?php
$md5_string = md5("hello world");
echo $md5_string;
?>
You can check if a string is an md5 string by comparing the two together, since it's a one-way hashing algorithm, this is called "colliding".
<?php
$password = md5("password");
$user_input = "password";
if(md5($user_input) == $password)
{
        echo "the password hash is <b>".$user_input."</b>.";
} else {
        echo "the password hash is NOT <b>".$user_input."</b>.";
}
?>
If you load a word list and loop through it, you have yourself a quick rough hash cracker.

Saturday, May 28, 2011

PHP Variables

PHP is a weakly typed language, so data types aren't a big deal. The hardest part is escaping strings, which is super simple. Here is a plain string variable, all variables always start with a dollar sign.
<?php
$my_string = "Hello World";
echo $my_string;
?>
 Here are some other ways to use and store data in variables.

<?php
$my_string = "Hello World";
$my_num = 122;
$my_string .= ", how are you";

echo $my_string;
?>