第一组PHP脚本

应学院网站组的要求,就算是完成任务吧,写了这几个PHP。

话说学校真无耻,本来说要培训的,突然又说不培训了,你们直接做吧。没办法,只好自己用了一上午现学,用了一下午现卖,弄出这么个成果。也许某年后的一天,我自己回来看这个,那一定是相当粗糙的。不过就目前而言,还是很有成就感的。

index.php

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>楠窗听雨</title>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="body">
        <?php include("header.php"); ?>
        <?php include("content.php"); ?>
        <?php include("sidebar.php"); ?>
    </div>
</body>
</html>

header.php

<div id="header">
    <a href="index.php">楠窗听雨</a>
</div>

content.php

<div id="content">
<?php
if (!isset($_COOKIE["user"]) or empty($_COOKIE["user"])) {
    if ($_GET["content"]=="") {
        echo "欢迎,请先登录。";
    }
    if ($_GET["content"]=="register") {
        include("register.php");
    }
} else {
    if ($_GET["content"]=="") {
        echo "欢迎您,".$_COOKIE["user"]."。";
    }
    if ($_GET["content"]=="userdetail") {
        include("userdetail.php");
    }
}
?>
</div>

sidebar.php

<div id="sidebar">
    <?php include("loginform.php"); ?>
</div>

userdetail.php

<div class='content'>
    <?php
    $sql=mysql_connect("localhost","test","test");
    if (!$sql)
        exit('无法连接数据库服务器。'.mysql_error());
    mysql_select_db("test", $sql);
    mysql_query("SET NAMES 'utf8'");
    $result = mysql_query("SELECT * FROM test WHERE username=trim('".$_GET["username"]."')");
    $userinfo = mysql_fetch_array($result,MYSQL_ASSOC);
    echo "以下是您的相关信息:<br /><br />";
    echo "用户名:".$userinfo['username']."<br />";
    echo "真实姓名:".$userinfo["realname"]."<br />";
    echo "年龄:".$userinfo["age"]."<br />";
    echo "性别:".$userinfo["sex"]."<br />";
    ?>
</div>

register.php

<div class='content'>
    <form name="registerform" action="login.php" method="post">
        用户名:<input type="text" name="username" />
        <br />
        密码:<input type="password" name="password" />
        <br />
        真实姓名:<input type="text" name="realname" />
        <br />
        年龄:<input type="text" name="age" />
        <br />
        性别:<input type="radio" name="sex" value="男" />男<input type="radio" name="sex" value="女" />女
        <br />
        <button name="action" type="submit" value="提交">提交</button>
    </form>
</div>

loginform.php

<div id="login">
    <?php if (!isset($_COOKIE["user"]) or empty($_COOKIE["user"])): ?>
    <form name="loginform" action="login.php" method="post">
        用户名:
        <br />
        <input type="text" name="username" />
        <br />
        密码:
        <br />
        <input type="password" name="password" />
        <br />
        <button name="action" type="submit" value="登陆">登陆</button>
        <button name="action" type="submit" value="注册">注册</button>
    </form>
    <?php else: ?>
    <form name="loginform" action="login.php" method="post">
        <?php echo $_COOKIE["user"] ?>,您已经登陆。
        <br /><br />
        <button name="action" type="submit" value="退出登录">退出登陆</button>
    </form>
    <?php endif; ?>
</div>

login.php

<?php
if ($_POST["action"]=="登陆") {
    $sql=mysql_connect("localhost","test","test");
    if (!$sql)
        exit('无法连接数据库服务器。');
    mysql_select_db("test", $sql);
    mysql_query("SET NAMES 'utf8'");
    $result=mysql_query("SELECT * FROM test WHERE trim(username)=trim('".$_POST["username"]."')");
    $userinfo=mysql_fetch_array($result,MYSQL_ASSOC);
    if ((trim($_POST["username"])==trim($userinfo["username"])) and (trim($_POST["password"])==trim($userinfo["password"])) and !trim($_POST["username"])=="") {
        setcookie("user",trim($userinfo["username"]),time()+1800);
        echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        echo "<meta http-equiv='refresh' content='3;url=index.php?content=userdetail&username=".$userinfo["username"]."'>";
        echo "</head><body>登录成功,页面将在3秒后跳转。";
    } else {
        echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        echo "<meta http-equiv='refresh' content='3;url=index.php?content=userdetail&username=".$userinfo["username"]."'>";
        echo "</head><body>登录失败,用户名或密码不正确。页面将在3秒后跳转。";
    }
}
if ($_POST["action"]=="注册") {
    echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    echo "<meta http-equiv='refresh' content='0;url=index.php?content=register'>";
    echo "</head><body>";
}
if ($_POST["action"]=="提交") {
    $sql=mysql_connect("localhost","test","test");
    if (!$sql)
        exit("无法连接数据库服务器。");
    mysql_select_db("test", $sql);
    mysql_query("SET NAMES 'utf8'");
    $result=mysql_query("SELECT COUNT(username) FROM test WHERE trim(username)='".trim($_POST["username"])."'");
    $userinfo=mysql_fetch_array($result,MYSQL_NUM);
    if ($userinfo[0]>0) {
        echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        echo "<meta http-equiv='refresh' content='3;url=index.php?content=register'>";
        echo "</head><body>该用户已存在。";
    } else {
        if (trim($_POST["username"])=="" or trim($_POST["password"])=="" or trim($_POST["realname"])=="" or trim($_POST["age"])=="" or trim($_POST["sex"])=="") {
            echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
            echo "<meta http-equiv='refresh' content='3;url=index.php?content=register'>";
            echo "</head><body>资料不完整。";
        } else {
            mysql_query("INSERT INTO test VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["realname"]."',".$_POST["age"].",'".$_POST["sex"]."')");    
            setcookie("user",trim($_POST["username"]),time()+1800);
            echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
            echo "<meta http-equiv='refresh' content='3;url=index.php?content=userdetail&username=".$_POST["username"]."'>";
            echo "</head><body>注册成功,页面将在3秒后跳转。";
        }
    }
}
if ($_POST["action"]=="退出登录") {
    setcookie("user","",time()-1800);
    echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    echo "<meta http-equiv='refresh' content='3;url=index.php'>";
    echo "</head><body>退出成功,页面将在3秒后跳转。";
}
?>
</body>
</html>

style.css

#body {
    position: relative;
    margin: auto;
    width: 800;
}

#header {
    position: absolute;
    left: 0px;
    top: 0px;
    width: 800px;
    height: 100px;
    text-align: center;
    font-size: 50px;
    border: 1px solid #000000;
}

#content {
    position: absolute;
    left: 0px;
    top: 100px;
    width: 600px;
    height: 400px;
    border: 1px solid #000000;
}

#sidebar {
    position: absolute;
    left: 600px;
    top: 100px;
    width: 200px;
    height: 400px;
    font-size: 10px;
    border: 1px solid #000000;
}

#login {
    position: relative;
    margin: 10px auto auto auto;
    width: 180px;
    font-size: 10px;
}

.content {
    position: relative;
    margin: 10px auto auto auto;
    width: 580px;
    font-size: 10px;
}

第一组PHP脚本》有4个想法

  1. /鼓掌 哇哈哈,其实挺不错滴啦。支持一下哈。
    前一段时间在找博客的模板,其实也看到了你的这种,但是感觉不太好看。
    昨晚到了你的博客,发现还不错。所以,我也在用这个模板了今天。
    8错!

    1. 这个模板是不错
      很简约的哈

      很多主题还是要看演示啊
      只看预览图总是达不到效果
      /呲牙

评论已关闭。