当前位置: 首页 > 图文教程 > 网络编程 > PHP > 十天学会php之第七天

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

PHP 中的 十天学会php之第七天


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 65 ::
收藏到网摘: n/a

  文本关键字:程序设计/PHP/技巧

  学习目的:学会SESSION的使用

  SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION;
  
  然后就可以使用SESSION变量了,比如说要赋值就是:$_SESSION['item']="item1";要得到值就是$item1=$_SESSION['item'];,很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某SESSION变量为空,可以这么写:empty($_SESSION['inum'])返回true or false。

  下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。
  
  登陆表单是这样:login.php
  <table width="100%" height="100%" border="0" align="center" cellpadding="0"   cellspacing="0">
  <tr>
  <form action="checklogin.php" method="post"><td align="center" valign="middle"><table   width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg">
  <tr class="tdbg">
  <td colspan="2"><div align="center">Administrators Login</div></td>
  </tr>
  <tr class="tdbg">
  <td><div align="center">Username</div></td>
  <td><div align="center">
  <input name="username" type="text" id="username">
  </div></td>
  </tr>
  <tr class="tdbg">
  <td><div align="center">Password</div></td>
  <td><div align="center">
  <input name="password" type="password" id="password">
  </div></td>
  </tr>
  <tr class="tdbg">
  <td colspan="2"><div align="center">
  <input type="submit" name="Submit" value="Submit">
  <input type="reset" name="Submit2" value="Clear">
  </div></td>
  </tr>
  </table></td></form>
  </tr>
  </table>

  处理文件是这样
  <?
  require_once('conn.php');
  session_start();
  $username=$_POST['username'];
  $password=$_POST['password'];
  $exec="select * from admin where username='".$username."'";
  if($result=mysql_query($exec))
  {
  if($rs=mysql_fetch_object($result))
  {
  if($rs->password==$password)
  {
  $_SESSION['adminname']=$username;
  header("location:index.php");
  }
  else
  {
  echo "<script>alert('Password Check Error!');location.href='login.php';</script>";
    }
  }
  else
  {
  echo "<script>alert('Username Check Error!');location.href='login.php';</script>";
  }
  }
  else
  {
  echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";
  }

  ?>

  conn.php是这样:
  <?
  $conn=mysql_connect ("127.0.0.1", "", "");
  mysql_select_db("shop");
  ?>

  由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:  checkadmin.asp
  <?
  session_start();
  if($_SESSION['adminname']=='')
  {
  echo "<script>alert('Please Login First');location.href='login.php';</script>";
  }
  ?>

  呵呵,今天说到这里,明天说一下怎么弄一个分页。