[英]PHP session variable won't change
UPDATE : I added all codes; 更新 :我添加了所有代码;
I want to create a login page for test purpose and I included attempt number with php sessions. 我想创建一个用于测试目的的登录页面,并且在php会话中添加了尝试编号。 The problem is session's variable isn't changing.
问题是会话的变量没有改变。
index.php <?php if (!isset($_SESSION)) { session_start(); $_SESSION['attempt']=3; } if ($_SESSION['attempt']<0){ header('location:login_error.php'); } require('../function/start.php'); $title = 'Login'; require('../template/header.php'); $ip = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; var_dump($_SESSION['attempt']) ?> <h3 class="epad_header">Login</h3> <div id="epad_wrapper"> <div id="ad_form_wrapper"> <form action="control.php" method="POST"> <fieldset> <span class="formtext">ID</span> <input type="text" name="epadName" id="epadName" required> <span class="formtext">Pass</span> <input type="password" name="epadPass" id="epadPass" required> <input type="submit" value="Ok" class="formbuton" name="formbuton"> <br> <span class="formtext">IP Adress : <?php echo $ip; ?></span> <br> <span class="formtext">Attempt : <?php echo $_SESSION['attempt'] ?></span> </fieldset> </form> </div> </div> <?php require('../template/footer.php'); ?>
control.php <?php if (!isset($_SESSION)) { session_start(); } require('../function/start.php'); $title = 'Control'; if (isset($_POST['formbuton'])){ $name = htmlentities($_POST['epadName']); $pwd = htmlentities($_POST['epadPass']); } $check = $db->prepare('SELECT user, pass FROM users'); $check->execute(); $result = $check->fetch(PDO::FETCH_ASSOC); $user = $result['user']; $pass = $result['pass']; if ($name == $user && $pwd == $pass ){ $_SESSION['name']= $name; header("location:main.php"); }else{ $_SESSION['attempt']--; header('location:index.php'); } ?>
when a user enter the page(first visit) ; 当用户进入页面(首次访问)时; a session will be started, and attempt variable will be created.
会话将开始,并且将创建try变量。 (index.php)
(index.php)
If they enter false data attempt variable will be decreased. 如果输入错误的数据,尝试变量将减少。 (control.php)
(control.php)
if there are 3 failed attempt page will redirects to login_error.page (index.php) 如果有3次失败的尝试页面将重定向到login_error.page(index.php)
First Problem : Session variable is not changing. 第一个问题:会话变量未更改。
Second Problem: Even If I enter correct data, the page directs to index.php (login area) instead of main.php 第二个问题:即使我输入正确的数据,页面也会直接指向index.php(登录区域)而不是main.php。
try to place session_start();
尝试放置
session_start();
at the first line, after <?php
在第一行,
<?php
I've solved the problem; 我已经解决了这个问题;
Actually I don't understand why 其实我不明白为什么
if(!isset($_SESSION){session_start; $_SESSION['attempt']=3;}
didn't work but when I modified this code to that code 没用,但是当我将此代码修改为该代码时
session_start;
if (!isset($_SESSION['attempt']){$_SESSION['attempt']=3;}
Anyway, thank you for reading. 无论如何,谢谢您的阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.