七月 25, 2010, 6:56 上午 作者: php培训讲师
这是一个php获得IP地址的函数,具体代码如下:
function get_ip() { //获取IP
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else if ($_SERVER["HTTP_CLIENT_IP"])
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
else if ($_SERVER["REMOTE_ADDR"])
{
$ip = $_SERVER["REMOTE_ADDR"];
}
else if (getenv("HTTP_X_FORWARDED_FOR"))
{
//PHP开源代码
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
else if (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
else if (getenv("REMOTE_ADDR"))
{
//PHP开源代码
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = "Unknown";
}
return $ip;
}
七月 10, 2010, 9:59 上午 作者: php培训讲师
define(‘BASE_PATH’,str_replace(‘\\’,'/’,realpath(dirname(__FILE__).’/../’)));
七月 4, 2010, 9:40 上午 作者: php培训讲师
/*—————————————————— */
//– 删除一篇文章生成的多个静态页面
//– 生成的文章名为 5.html 5_2.html 5_3.html
/*—————————————————— */
function delStaticHtml ($article_id)
{
global $db;
$sql = “SELECT `post_time` FROM `@__article` WHERE `article_id` = ‘{$article_id}’”;
$art = $db->getOne ($sql);
if ($art)
{
$n = 1;
$html_dir = ‘../html/’.date(‘Ym’,$art['post_time']).’/';
$filename = $html_dir.$article_id.’.html’;
while (file_exists($filename))
{
@unlink($filename);
$n++;
$filename = $html_dir.$article_id.’_’.$n.’.html’;
}
}
return false;
}
七月 4, 2010, 1:49 上午 作者: php培训讲师
/*—————————————————— */
//– 获取无限分类ID下面的子类ID集
//– $sort_id = $sort_id.getChildrenIds($sort_id);
//– $sql = ” ….. where sort_id in ($sort_id)”;
/*—————————————————— */
function getChildrenIds ($sort_id)
{
global $db;
$ids = ”;
$sql = “SELECT * FROM “.$db->table(‘article_sort’).” WHERE `parent_id` = ‘{$sort_id}’”;
$res = $db->query ($sql);
if ($res)
{
while ($row = $db->fetch_assoc ($res))
{
$ids .= ‘,’.$row['sort_id'];
$ids .= getChildrenIds ($row['sort_id']);
}
}
return $ids;
}
七月 4, 2010, 1:39 上午 作者: php培训讲师
说明:目录下必须有default.gif(此图为模板缩略图)的才为合法的模板
/*—————————————————— */
//– 自动获取模板函数
/*—————————————————— */
function get_template ()
{
$template = array ();
$dir = CMS_ROOT.’/tpl/’;
$n = 0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file ==’.’ or $file == ‘..’ or $file == ‘.svn’)
{
continue;
}
if (is_dir ($dir.$file))
{
if (file_exists ($dir.$file.’/default.gif’))
{
$template[$n]['dir'] = $file;
$template[$n]['pic'] =’/tpl/’.$file.’/default.gif’;
}
}
$n++;
}
closedir($dh);
}
}
return $template;
}
七月 4, 2010, 1:26 上午 作者: php培训讲师
/*—————————————————— */
//– 递归删除文件及目录
//– 例: del_dir (‘../cache/’);注意:返回的/是必须的
//– $type 强制删除目录, true 是 ,false 否
/*—————————————————— */
function del_dir ($dir,$type=true)
{
$n=0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//.svn 忽略 svn 版本控制信息
if ( $file == ‘.’ or $file ==’..’ or $file == ‘.svn’)
{
continue;
}
if (is_file ($dir.$file))
{
unlink($dir.$file);
$n++;
}
if (is_dir ($dir.$file))
{
del_dir ($dir.$file.’/');
if ($type)
{
$n++;
rmdir($dir.$file.’/');
}
}
}
}
closedir($dh);
}
return $n;
}
七月 3, 2010, 4:15 下午 作者: php培训讲师
/*—————————————————— */
//– 获取无限分类的列表数据
/*—————————————————— */
function get_sort ($parent_id=0,$n=-1)
{
global $db;
static $sort_list = array ();
$sql = “SELECT * FROM “.$db->table(‘article_sort’).” WHERE `parent_id` = ‘{$parent_id}’”;
$res = $db->query ($sql);
if ($res)
{
$n++;
while ($row = $db->fetch_assoc ($res))
{
$sql = “SELECT * FROM “.$db->table(‘article_sort’).” WHERE `parent_id` = ‘{$row['sort_id']}’”;
$children = $db->num_rows ($sql);
$row['sort_name'] = str_repeat (‘ ’,$n*4).$row['sort_name'];
$row['children'] = $children;
$sort_list[] = $row;
get_sort ($row['sort_id'],$n);
}
}
return $sort_list;
}
七月 3, 2010, 4:02 下午 作者: php培训讲师
/*—————————————————— */
//– 递归实现无限分类生成下拉列表函数
//– $tpl->assign(’sort_list’,createSortOptions ());
//– $tpl->assign(’sort_list’,createSortOptions ($sort_id));
/*—————————————————— */
function createSortOptions ($selected=0,$parent_id=0,$n=-1)
{
global $db;
$sql = “SELECT * FROM `@__article_sort` WHERE `parent_id` = ‘{$parent_id}’”;
$options = ”;
static $i = 0;
if ($i == 0)
{
$options .= ‘<option value=”0″ >请选择</option>’;
}
$res = $db->query ($sql);
if ($res)
{
$n++;
while ($row = $db->fetch_assoc ($res))
{
$i++;
$options .=”<option value=’{$row['sort_id']}’”;
if ($row['sort_id'] == $selected)
{
$options .=’ selected ‘;
}
$options .=”>”.str_repeat(‘ ’,$n*3).$row['sort_name'].”</option>\n”;
$options .=createSortOptions ($selected,$row['sort_id'],$n);
}
}
return $options;
}
七月 3, 2010, 3:51 下午 作者: php培训讲师
/*—————————————————— */
//– 简单提示框函数
/*—————————————————— */
function alert ($msg,$url=”)
{
$str = ‘<script type=”text/javascript”>’;
$str.=”alert(‘”.$msg.”‘);”;
if ($url != ”)
{
$str.=”window.location.href=’{$url}’;”;
}
else
{
$str.=”window.history.back();”;
}
echo $str.=’</script>’;
}
七月 3, 2010, 3:37 下午 作者: php培训讲师
/*—————————————————— */
//– 检测用户是否登录函数
/*—————————————————— */
function checklogin ()
{
$user_id = isset($_SESSION['admin']['uid']) ? intval($_SESSION['admin']['uid']) : 0;
if ($user_id < 1)
{
exit (“<script>alert (‘对不起,你还没有登录,请先登录’);window.parent.location.href=’login.php’;</script>”);
//parent ,防止框架页,跳出不成功。
}
}