birthday format is YYYY-MM-DD
function age($birthDay) {
list($y, $m, $d) = explode('-', $birthDay);
if (($m = (date('m') - $m)) < 0) {
$y++;
} elseif ($m == 0 && date('d') - $d < 0) {
$y++;
}
return date('Y') - $y;
}
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Tuesday, November 6, 2012
Friday, October 19, 2012
Get datetime with php
how to get datetime with php?
$datetime = date('Y-m-d H:i:s');
echo $datetime;
$datetime = date('Y-m-d H:i:s');
echo $datetime;
Get user IP with PHP
How to get user ip with php?
Example:
$userIp = $_SERVER['REMOTE_ADDR'];
echo $userIp;
Example:
$userIp = $_SERVER['REMOTE_ADDR'];
echo $userIp;
php generate random number
How to generate randum number with php
$rand = rand(1, 1000); //generate rand number from 1 to 1000
echo $rand;
$rand = rand(1, 1000); //generate rand number from 1 to 1000
echo $rand;
php BAN system
php BAN system
How to create easy ip ban system with PHP?
system for ban user ip
CREATE TABLE `bans` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`ip` CHAR( 15 ) NOT NULL ,
) ENGINE = MYISAM
example template:
<div>
<form action="" method="post">
Ban IP: <input type="text" name="ip" value="***.***.***.***" /> <br />
<input type="submit" name="ban" value="ban this ip" />
</form>
</div>
php code:
<?php
if(isset($_POST['ban'])){
mysql_query("
INSERT INTO
users (ip)
VALUES
('" . $_POST['ip'] . "')
");
}
?>
function checkUserBan(){
$checkUserBan_sql = "
SELECT
*
FROM
bans
";
$checkUserBan_res = mysql_query($checkUserBan_sql);
$banIp = array();
while($checkBans = mysql_fetch_array($checkUserBan_res)){
$banIp[] = $checkBans['ip'];
}
if(in_array($_SERVER['REMOTE_ADDR'], $banIp)){
Header('Location: /bans.php');
exit;
}
}
paste function "checkUserBan();" at the top of every file on your site without bans.php
How to create easy ip ban system with PHP?
system for ban user ip
CREATE TABLE `bans` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`ip` CHAR( 15 ) NOT NULL ,
) ENGINE = MYISAM
example template:
<div>
<form action="" method="post">
Ban IP: <input type="text" name="ip" value="***.***.***.***" /> <br />
<input type="submit" name="ban" value="ban this ip" />
</form>
</div>
php code:
<?php
if(isset($_POST['ban'])){
mysql_query("
INSERT INTO
users (ip)
VALUES
('" . $_POST['ip'] . "')
");
}
?>
function checkUserBan(){
$checkUserBan_sql = "
SELECT
*
FROM
bans
";
$checkUserBan_res = mysql_query($checkUserBan_sql);
$banIp = array();
while($checkBans = mysql_fetch_array($checkUserBan_res)){
$banIp[] = $checkBans['ip'];
}
if(in_array($_SERVER['REMOTE_ADDR'], $banIp)){
Header('Location: /bans.php');
exit;
}
}
paste function "checkUserBan();" at the top of every file on your site without bans.php
PHP Get image size and type - width, height, type.
Get image size and type - width, height, type.
<?php
list($width, $height, $type) = getimagesize('/path/to/your/image/image.jpg');
echo $widht . ' - ' . $height . ' - ' . $type;
?>
<?php
list($width, $height, $type) = getimagesize('/path/to/your/image/image.jpg');
echo $widht . ' - ' . $height . ' - ' . $type;
?>
Php function check username
Php function check username
function checkUsername($username){
if(preg_match("/^[a-zA-Z0-9-_]+$/", $username)) {
if(strlen($username) >= 3){
if(empty($result)){
$result = 'success';
}
}else{
$result = 'invalid';
}
}else{
$result = 'invalid';
}
return $result;
}
<?php
if(checkUsername('YourUsername') == 'success'){
echo 'Username is correct!';
}else{
echo 'Invalid Username';
}
?>
function checkUsername($username){
if(preg_match("/^[a-zA-Z0-9-_]+$/", $username)) {
if(strlen($username) >= 3){
if(empty($result)){
$result = 'success';
}
}else{
$result = 'invalid';
}
}else{
$result = 'invalid';
}
return $result;
}
<?php
if(checkUsername('YourUsername') == 'success'){
echo 'Username is correct!';
}else{
echo 'Invalid Username';
}
?>
php register system
How to make user register system with username checker?
1. Create table with 5 rows.
example: table name = users
CREATE TABLE `users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`password` CHAR( 32 ) NOT NULL ,
`username` VARCHAR( 50 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
`real_name` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM
example register.php file:
<?php
if(isset($_POST['register'])){
$checkUsername_sql = "
SELECT
username
FROM
users
WHERE
username = '" . $_POST['username'] . "'
";
$checkUsername_res = mysql_query($checkUsername_sql);
$checkUsername = mysql_fetch_array($checkUsername_res);
$getUsername = $checkUsername['username'];
if(!$getUsername){
mysql_query("
INSERT INTO
users (username, password, email, real_name)
VALUES
('" . $_POST['username'] . "', '" . md5($_POST['password']) . "', '" . $_POST['email'] . "', '" . $_POST['real_name'] . "')
");
$result = 'Success';
}else{
$result = 'This username exist!';
}
}
?>
example register template file:
<html lang="en">
<head>
<title>Sample HTML File</title>
</head>
<body bgcolor="white">
<?
if(isset($_POST['register']) AND isset($result)){?>
echo $result;
}
?>
<div>
<form action="" method="post">
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
E-mail: <input type="text" name="email" /> <br />
Real Name: <input type="text" name="real_name" /> <br />
<input type="submit" name="register" value="Register" />
</form>
</div>
</body>
</html>
1. Create table with 5 rows.
example: table name = users
CREATE TABLE `users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`password` CHAR( 32 ) NOT NULL ,
`username` VARCHAR( 50 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
`real_name` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM
example register.php file:
<?php
if(isset($_POST['register'])){
$checkUsername_sql = "
SELECT
username
FROM
users
WHERE
username = '" . $_POST['username'] . "'
";
$checkUsername_res = mysql_query($checkUsername_sql);
$checkUsername = mysql_fetch_array($checkUsername_res);
$getUsername = $checkUsername['username'];
if(!$getUsername){
mysql_query("
INSERT INTO
users (username, password, email, real_name)
VALUES
('" . $_POST['username'] . "', '" . md5($_POST['password']) . "', '" . $_POST['email'] . "', '" . $_POST['real_name'] . "')
");
$result = 'Success';
}else{
$result = 'This username exist!';
}
}
?>
example register template file:
<html lang="en">
<head>
<title>Sample HTML File</title>
</head>
<body bgcolor="white">
<?
if(isset($_POST['register']) AND isset($result)){?>
echo $result;
}
?>
<div>
<form action="" method="post">
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
E-mail: <input type="text" name="email" /> <br />
Real Name: <input type="text" name="real_name" /> <br />
<input type="submit" name="register" value="Register" />
</form>
</div>
</body>
</html>
Read file with php
How to read file with php?
example:
Text file name - example.txt
php code:
<?php
$file = '/path/to/file/example.txt';
foreach($file AS $row){
echo $row . "\n";
}
?>
example:
Text file name - example.txt
php code:
<?php
$file = '/path/to/file/example.txt';
foreach($file AS $row){
echo $row . "\n";
}
?>
Thursday, October 11, 2012
Convert text with iconv
Convert from UTF-8 to windows-1251
example:
example:
$text = 'Hello world';
iconv('utf8', 'windows-1251', $text);
cutting text with php
function abbreviate($text, $val){
$text = iconv('utf8', 'windows-1251', $text);
if(strlen($text) > $val){
$text = substr($text, 0, $val) . '...';
$text = iconv('windows-1251', 'utf8', $text);
}else{
$text = iconv('windows-1251', 'utf8', $text);
}
return $text;
}
$text = iconv('utf8', 'windows-1251', $text);
if(strlen($text) > $val){
$text = substr($text, 0, $val) . '...';
$text = iconv('windows-1251', 'utf8', $text);
}else{
$text = iconv('windows-1251', 'utf8', $text);
}
return $text;
}
How to List A Directory Contents Using PHP (Recursive)
function listDirectory($path) { $handle = @opendir($path); while (false !== ($file = readdir($handle))) { if ($file == '.' || $file == '..') continue; if ( is_dir("$path/$file")) { echo "$path/$file\n"; listDirectory("$path/$file"); } else { echo "$path/$file\n"; } } closedir($handle); } listDirectory("tutorials");
Subscribe to:
Posts (Atom)