php中利用session验证登录表单,获取表单数据方法

作者: 金沙澳门官网  发布:2019-09-26

在php教程收获表单数占领专项使用的通令, $_POST[]正是那个函数了,上面我们二个大约的实例

登入页面是:

<?php
echo 'Hello, ' . $_POST['first_name'] . '!';
?>
 
出口是来自表单的first_name的值。

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<form name="login" action="login.php" method="post">
姓名:<input type=text name="name"><br/>
密码:<input type=password name="password"><br/>
<!-- <input type="radio" name="limits" value="1">管理员 -->
<!-- <input type="radio" name="limits" value="0">普通顾客-->
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>

再繁杂一小点的实例,

存储session的页面:

$_POST['name'] = trim($_POST['name']);

if (strlen($_POST['name']) == 0) {
    $errors[] = "Your name is required.";
}

<?php
header("Content-Type: text/html; charset=utf8");
if( !isset($_POST["submit"]) ){
die("错误实践");
}//检查实验是不是有submit操作
 
require_once('connect.php');//链接数据库
 
if ( isset($_POST['name']) && isset($_POST['password']) ){//如若顾客名和密码都不为空
 
$name = $_POST['name'];
 
$password = $_POST['password'];
 
$sql = " SELECT id, limits, message FROM user WHERE username = '$name' AND password = '$password' LIMIT 1";
 
$result = mysqli_query( $con , $sql );//试行sql 客户名和密码
 
$rows = mysqli_num_rows( $result );//再次回到客商名密码是不是存在
 
if( $rows != 0 ){
 
session_start();
 
while( $rows_other = mysqli_fetch_assoc($result) ){
 
$_SESSION['id'] = $rows_other['id'];
$_SESSION['name'] = $name;
$_SESSION['limits'] = $rows_other['limits'];
$_SESSION['message'] = $rows_other['message'];
 
}
 
header("refresh:0;url=welcome.php");//跳转至welcome.html页面
 
exit;
 
}else{
 
echo "顾客名或密码错误";
 
echo "<script>
alert('客商名或密码错误');
setTimeout(function(){window.location.href='login.html';},1000);
</script>";
 
}
 
}else{
 
echo "表单填写不完全";
 
echo "<script>
alert('表单填写不完整');
setTimeout(function(){window.location.href='login.html';},1000);
</script>";
 
}
?>

好了,上面做一个关于$_POST的完全实例

登入后跳转的页面,依照不一样的客商显示不相同的权柄和客商名:

<?php
  $user =  $_POST['user'];
  $color = $_POST['color'];
  $self =  $_SERVER['PHP_SELF'];

  if( ( $user != null ) and ( $color != null ) )
  {
    setcookie( "firstname", $user , time() + 36000 );
    setcookie( "fontcolor", $color, time() + 36000 );
    header( "Location:getcookie.php" );
    exit();
  }
?>

 

<html>

 <head>
  <title>Set Cookie Data</title>
 </head>

 <body>

  <form action ="<?php echo( $self ); ?>" method = "post">

  Please enter your first name:
  <input type = "text" name = "user"><br><br>

  Please choose your favorite font color:<br>
  <input type = "radio" name = "color" value = "#FF0000">Red
  <input type = "radio" name = "color" value = "#00FF00">Green
  <input type = "radio" name = "color" value = "#0000FF">Blue
  <br><br>
  <input type = "submit" value = "submit">
  </form>

 </body>

</html>

<?php
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
 
<?php
 
session_start();
 
if( isset($_SESSION['id']) ){
 
require_once('connect.php');
 
$id = $_SESSION['id'];
$name = $_SESSION['name'];
$limits = $_SESSION['limits'];
$message = $_SESSION['message'];
 
if( $limits == 1 ){
 
echo 'hello, 管理员' . '<br/>';
 
}else{
 
echo 'helo, 普通用户' . '<br/>';
}
 
echo 'hello you name is:' . $name;
 
}else{
 
echo '未登录!';
 
header("refresh:3;url=login.html");
 
}
?>
 
 
</body>
</html>
?>

 
小结:上边的实例没做安全管理,只是简单的获取了表单提交的多寡,大家得以做一些安然无事管理,如isset() addslashes 等拍卖。

选择session注意事项

1.在日前页面要运用session时大家在文件最前头未有输入内容时抬高session_start();

2.session有贰个时日范围的这些大家能够张开更动的,具体如下

 其实PHP5 Session还提供了三个函数 session_set_cookie_params(); 来设置PHP5 Session的生存期的,该函数必需在 session_start() 函数调用在此以前调用:

<?php
    // 保存一天
    $lifeTime = 24 * 3600;
    session_set_cookie_params($lifeTime);
    session_start();
?>

本文由金沙澳门官网送注册58发布于金沙澳门官网,转载请注明出处:php中利用session验证登录表单,获取表单数据方法

关键词: