Re: Як не варто писати код. Поганий VS Хороший код.
Знайшов свій бидло-код річної давності.
▼Прихований текст
<?php
// MYSQL Connect
function db_connect()
{
include_once($_SERVER['DOCUMENT_ROOT']."/engine/config/db.php");
@$link = mysql_connect(DBHOST,DBUSER,DBPASS);
if($link==false)
{ echo '<script>
document.location.href="/error.php?id=1";
</script>';exit; }
@$sd = mysql_select_db(DBNAME);
if($sd==false)
{ echo '<script>
document.location.href="/error.php?id=2";
</script>';exit; }
@mysql_query("SET NAMES cp1251");
}
//View static page
function view_page($page)
{
db_connect();
if(!eregi("^['a-zA-Z0-9']+['a-zA-Z0-9']+$",$page))
{ echo '<script>
document.location.href="/error.php?id=3";
</script>';exit; }
$sdp = mysql_query("SELECT title,keywords,description,text FROM page WHERE name='$page'");
if(mysql_num_rows($sdp)==0)
{ echo '<script>
document.location.href="/error.php?id=3";
</script>'; exit;}
$datapage = mysql_fetch_array($sdp);
include_once($_SERVER['DOCUMENT_ROOT']."/tpl/static.html");
}
//Registration user
function reg_user()
{
$login = $_POST['login'];
$pass = $_POST['password'];
$email = $_POST['email'];
if(!empty($login) and !empty($pass) and !empty($email))
{
if(!eregi("^[a-zA-Z0-9]+[a-zA-Z0-9]+$",$login))
{ echo '<script>document.location.href="/error.php?id=4";</script>'; exit;
}
if(!eregi("^[a-zA-Z0-9]+[a-zA-Z0-9]+$",$pass))
{ echo '<script>document.location.href="/error.php?id=5";</script>'; exit;
}
if(!eregi("^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",$email))
{ echo '<script>document.location.href="/error.php?id=6";</script>'; exit;
}
//вставка в бд
$login = htmlspecialchars($login);
$pass = md5($pass);
db_connect();
$insertnewuser = mysql_query("INSERT INTO user(banned,reason,login,password,email) VALUE ('false','Порушення правил!','$login','$pass','$email')");
if($insertnewuser==FALSE)
{ echo '<script>
document.location.href="/error.php?id=7";
</script>'; exit;
}
else
{ echo '<script>
document.location.href="/auth.php";
</script>'; exit;
}}
include($_SERVER['DOCUMENT_ROOT']."/tpl/register.html");
}
//Auth user
function auth_user()
{
$login = $_POST['login'];
$pass = $_POST['password'];
if(!empty($login) and !empty($pass))
{
if(!eregi("^[a-zA-Z0-9]+[a-zA-Z0-9]+$",$login))
{ echo '<script>document.location.href="/error.php?id=4";</script>'; exit; }
if(!eregi("^[a-zA-Z0-9]+[a-zA-Z0-9]+$",$pass))
{ echo '<script>document.location.href="/error.php?id=5";</script>'; exit; }
db_connect();
$login = htmlspecialchars($login);
$pass = md5($pass);
$selectdatauser = mysql_query("SELECT id FROM user WHERE login='$login' and
password='$pass'");
if(mysql_num_rows($selectdatauser)==0)
{ echo '<script>document.location.href="/error.php?id=8";</script>'; exit; }
$_SESSION['valid_user'] = $login;
echo '<script>document.location.href="/account.php";</script>'; exit;
}
else
{
include($_SERVER['DOCUMENT_ROOT']."/tpl/auth.html");
}}
function check_auth_user()
{
if(isset($_SESSION['valid_user']))
{
echo '<script>document.location.href="/account.php";</script>'; exit;
}}
function data_user()
{
$login = $_SESSION['valid_user'];
db_connect();
$Selectdatauser = mysql_query("SELECT id,banned,reason,email FROM user WHERE login='$login'");
if(mysql_num_rows($Selectdatauser)==0)
{ echo '<script>document.location.href="/auth.php";</script>'; exit; }
$DataUser = mysql_fetch_array($Selectdatauser);
include($_SERVER['DOCUMENT_ROOT']."/tpl/account.html");
}
//List foto
function list_foto()
{
db_connect();
if(isset($_GET['page']) and !empty($_GET['page']) and eregi("^[0-9]+$",$_GET['page']))
{
$page = $_GET['page']*10;
$selectfoto = mysql_query("SELECT `id`,`name`,`link`,`desc`,`view` FROM foto WHERE p=1 ORDER BY `id` DESC LIMIT $page,10 ");
if(mysql_num_rows($selectfoto) == 0)
{ echo '<script>
document.location.href="/foto.php";
</script>';exit;
}
else
{
$df = mysql_fetch_array($selectfoto);
do
{
printf('
<p> <br /><center><a href="/view_foto.php?id=%s" class="capsule">%s</a>
<img src="/uploads/foto/%s" width="" height="" border="0"><br />
%s<br />
Переглядів: %s</center>
</p>
',$df['id'],$df['name'],$df['link'],$df['desc'],$df['view']);
}
while($df = mysql_fetch_array($selectfoto));
} }
else
{
$selectfoto = mysql_query("SELECT `id`,`name`,`link`,`desc`,`view` FROM foto WHERE p=1 ORDER BY `id` DESC LIMIT 0,10 ");
$df = mysql_fetch_array($selectfoto);
if(mysql_num_rows($selectfoto) == 0)
{ echo "Фотографій нема!"; return; }
do
{
printf('
<p> <br /><center><a href="/view_foto.php?id=%s" class="capsule">%s</a>
<img src="/uploads/foto/%s" width="" height="" border="0"><br />
%s<br />
Переглядів: %s</center>
</p>
',$df['id'],$df['name'],$df['link'],$df['desc'],$df['view']);
}
while($df = mysql_fetch_array($selectfoto));
}
$selectfoto = mysql_query("SELECT `id` FROM foto WHERE p=1 ");
$numrows = mysql_num_rows($selectfoto);
$numpage = $numrows / 10;
$numpage = $numpage + 1;
@$ex = explode(".",$numpage);
$p = $ex['0'];
if(!isset($ex['1']))
{ $p = $p-1; }
$b = 1;
echo " <h3>Перейти на сторінку: ";
for($i=0;$i<$p;$i++)
{
printf('<a href="/foto.php?page=%s"> %s </a>',$i,$b);
$b++;
} echo "</h3>"; }
//View foto
function view_foto()
{
if(isset($_GET['id']) and !empty($_GET['id']) and eregi("^[0-9]+$",$_GET['id']))
{
$id = $_GET['id'];
db_connect();
$selectfoto = mysql_query("SELECT `name`,`link`,`desc`,`view` FROM foto WHERE id='$id'");
if(mysql_num_rows($selectfoto)==0)
{ echo '<script>
document.location.href="/foto.php";
</script>';exit; }
$df = mysql_fetch_array($selectfoto);
printf('<br />%s<br />
<img src="/uploads/foto/%s" width="250" height="250"><br />
%s<br />%s
',$df['name'],$df['link'],$df['desc'],$df['view']);
}
else
{
echo '<script>
document.location.href="/foto.php";
</script>';exit;
}}
//List news
function list_post()
{
db_connect();
if(isset($_GET['page']) and !empty($_GET['page']) and eregi("^[0-9]+$",$_GET['page']))
{
$page = $_GET['page']*10;
$selectpost = mysql_query("SELECT `id`,`title`,`short_news`,`author` FROM post WHERE p=1 ORDER BY `id` DESC LIMIT $page,10 ");
if(mysql_num_rows($selectpost) == 0)
{ echo '<script>
document.location.href="/post.php";
</script>';exit;
}
else
{
$dp = mysql_fetch_array($selecpost);
do
{
printf('
<p> <br /><center><a href="/view_post.php?id=%s" class="capsule">%s</a>
%s
Автор: %s
</center>
</p>
',$dp['id'],$dp['title'],$dp['short_news'],$dp['author']);
}
while($dp = mysql_fetch_array($selecpost));
} }
else
{
$selecpost = mysql_query("SELECT id,title,short_news,author FROM post WHERE p=1 ORDER BY id DESC LIMIT 0,10");
if(mysql_num_rows($selecpost) == 0)
{ echo "Матеріалів нема!"; return; }
$dp = mysql_fetch_array($selecpost);
do
{
printf('
<p> <br /><center><a href="/view_post.php?id=%s" class="capsule">%s</a>
%s<br /><br />
Автор: %s
</center>
</p>
',$dp['id'],$dp['title'],$dp['short_news'],$dp['author']);
}
while($dp = mysql_fetch_array($selecpost));
}
$selecpost = mysql_query("SELECT `id` FROM post WHERE p=1 ");
$numrows = mysql_num_rows($selecpost);
$numpage = $numrows / 10;
$numpage = $numpage + 1;
@$ex = explode(".",$numpage);
$p = $ex['0'];
if(!isset($ex['1']))
{ $p = $p-1; }
$b = 1;
echo " <br /><h3>Перейти на сторінку: ";
for($i=0;$i<$p;$i++)
{
printf('<a href="/post.php?page=%s">%s</a>',$i,$b);
$b++;
} echo "</h3>"; }
//View post
function view_post()
{
if(isset($_GET['id']) and !empty($_GET['id']) and eregi("^[0-9]+$",$_GET['id']))
{
$id = $_GET['id'];
db_connect();
$selectpost = mysql_query("SELECT `title`,`full_news`,`author`,`tags` FROM post WHERE id='$id'");
if(mysql_num_rows($selectpost)==0)
{ echo '<script>
document.location.href="/post.php";
</script>';exit; }
$dp = mysql_fetch_array($selectpost);
printf('
%s<br />%s<br />%s<br />%s<br />
',$dp['title'],$dp['full_news'],$dp['author'],$dp['tags']);
}
else
{
echo '<script>
document.location.href="/post.php";
</script>';exit;
}}
//List comments
function list_comments()
{
$id = $_GET['id'];
db_connect();
$selectcom = mysql_query("SELECT name,text FROM comments WHERE newsid='$id'");
if(mysql_num_rows($selectcom)==0)
{ echo "<br /><br />Коментарів немає!"; return; }
$dc = mysql_fetch_array($selectcom);
do
{
printf('
<br />Додав: %s<br />
%s<br />
',$dc['name'],$dc['text']);
}
while($dc = mysql_fetch_array($selectcom));
}
//Add comments
//function add_comments()
//{
//if(isset($_POST['name']) and isset($_POST['text']) and !empty($_POST['name']) and !empty($_POST['text']))
//{
//if(!eregi("^[a-zA-zа-яА-Я0-9]+$",$_POST['name']))
//{
//}
//}
?>