登陆页面
<html>
<h1>登陆页面</h1></br>
<form action="login.php" method="post">
用户名:
<input type="text" name="userName" /><br/>
密 码:
<input type="password" name="userPassword" /><br/>
<input type="submit" name="submit" value="登陆" />
<!-- <a> 标签定义超链接,用于从一张页面链接到另一张页面 -->
<!-- <a> 元素最重要的属性是 href 属性,它指示链接的目标 -->
</form>
</html>
<a href="register.php">注册</a>
<?php
session_start(); //开启session回话
include("connect.php"); //连接mysql数据库
$name=$_POST['userName']; //保存从login中post传过来的数据
echo $name;
$password=$_POST['userPassword']; //保存从logoin中post传过来的数据
$sql="select username, userpassword from userlist where username='$name' and userpassword='$password'"; //mysql查询语句
$result=mysql_query($sql); //保存查询到的内容
$num=mysql_num_rows($result); //返回查询的记录条数
if($num){
$_SESSION["name"]=$name; //注册一个名为name的变量
$row=mysql_fetch_array($result);
echo '<script>window.location.href="viewPicture.php"</script>';
}else{
echo '用户名或密码错误 <a href="register.php">单击进入用户注册</a>'; //如果密码或者用户名错误返回上一页
}
?>
注册页面
<html>
<h1>注册页面</h1></br>
<form action="register.php" method="post">
用户名
<input type="text" name="username" /><br/>
密码
<input type="text" name="userpassword" /><br/>
<input type="submit" name="sub" /><br/>
</form>
</html>
<?php
session_start();
include("connect.php");
@$name=$_POST['username'];
@$password=$_POST['userpassword'];
$sql="select username, userpassword from userlist where username='$name'";
$result=mysql_query($sql);
$num_rows=mysql_num_rows($result);
if($num_rows){
echo "the name is existed";
}else{
$_SESSION["name"]=$name;
$insertSql="insert into userlist(`id`,`username`,`userpassword`,`creattime`) values(null,'$name','$password',now())";
mysql_query($insertSql);
$path="picture/".$name;
mkdir($path,0770);
echo "success,go to log in";
// echo '<script>window.location.href="viewPicture.php"</script>';
}
?>
上传图片
<html>
<h1>图片上传</h1></br>
<form action="loadPicture.php" method="post" enctype="multipart/form-data">
选择图片:
<input type="file" name="file" id="file" /></br>
<input type="submit" name="submit" value="上传" /></br>
</form>
</html>
<?php
//loadPictur.php
session_start(); //开启session回话
if($_SESSION['NAME']==NULL)
{
echo "您还没有登陆".'</br>';
echo '<a href="login.php">去登陆</a>'.'</br>';
}
else{
include("connect.php"); //链接数据库
$pictureSize=20000000; //图片最大20M左右
echo "test";
//判断图片是否合法
if((($_FILES['file']['type']=='image/gif')||
($_FILES['file']['type']=='image/jpeg')||
($_FILES['file']['type']=='image/pjpeg'))&&
($_FILES['file']['size']<$pictureSize))
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"]."<br />";
}else{
//判断文件是否存在
if(file_exists("picture/".$_SESSION['name']."/".$_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"]."已经存在";
}
else{
//将图片保存到用户的路径下
move_uploaded_file($_FILES["file"]["tmp_name"],
"picture/".$_SESSION['name']."/".$_FILES["file"]["name"]);
//将图片信息保存到数据库中pictures表中
$userPictureName=basename($_FILES["file"]["name"]);//保存当前图片名字
$userName=$_SESSION["name"];
$userPictureLocation="picture/".$_SESSION['name']."/".$_FILES["file"]["name"];
$sql="insert into `pictures` values(null, '$userName', '$userPictureName', '$userPictureLocation', now())";
mysql_query($sql);
echo "上传完毕";
echo '<a href="viewPicture.php">去查看图片</a>';
}
}
}else{
echo "文件不符合要求,请检查文件大小或者格式".'</br>';
}
}
?>
查看图片
<html>
<h1>图片浏览</h1></br>
<h3>去上传图片</h3>
<a href="loadPicture.php">上传图片</a>
</br>
</br>
</br>
<h3></h3>
</html>
<?php
//vewPicture
session_start(); //开启session回话
if($_SESSION['NAME']==NULL)
{
echo "您还没有登陆".'</br>';
echo '<a href="login.php">去登陆</a>'.'</br>';
}
else{
include("connect.php");
echo "欢迎您,".$_SESSION['name'].'</br>';
$guest_name=$_SESSION['name'];
//从数据库中查询用户的图片信息
$sql="select * from pictures where user_name='$guest_name'";
$Result=mysql_query($sql);
$Row_num=mysql_num_rows($Result);
echo $Row_num.'</br>';
//判断是否上传了相片
if($Result){
while($row_array=mysql_fetch_array($Result,MYSQL_NUM)){
echo "上传者:".$row_array[1]."</br>";
echo "图片名称:".$row_array[2]."</br>";
echo "上传时间: ".$row_array[4]."<br>";
echo $row_array[3]."</br>";
echo "<img src='$row_array[3]'>"."</br>";
}
mysql_free_result($Result); //释放与之关联的资源
}
else{
echo "抱歉!您之前没有上传图片"."</br>";//如果没有上传,给出提示并给出上传的链接
echo '<a hreaf="loadPicture.php">现在去上传</a>';
}
}
?>
数据库连接
<?php
@mysql_connect("localhost","root","")or die("mysql连接失败"); //以root用户登录数据库,如果登陆失败给出提示
@mysql_select_db("xc")or die("数据库连接失败"); //选择名字为'xc'的数据库
mysql_query("set names 'gbk'"); //告知服务器客户端的编码是gbk,希望返回gbk编码的查询结果
?>
说明:改程序没有实现删除图片功能。另外在一些编码细节方面还有待处理的地方。
新手入门,希望得到您的指导意见!