简单的PHP登录:ID不会传递到页面,但其他一切都会

时间:2014-11-20 21:47:51

标签: php html

我能够传递用户名但由于某种原因我无法传递ID。从第1页到第2页。在第2页中,我在html体中回显了$ result。登录屏幕上的名称将被传递,但当我显示时,id为空白。出现id:name:myName

1页

   <?php 

  session_start();
   if(isset($_POST['submit']))      {
  $dbu = "myName";
  $dbp = "abc123";
 $uid = "1111";

 $name = $_POST['user'];
   $pass = $_POST['pass'];

if($name == $dbu && $pass == $dbp)
  {
    $_SESSION['user'] = $name;
    $_SESSION['id'] = $uid;
    header("Location: test.php");
  }
  else
  {    header("Location: unsuccessful.php");    }
}  
 ?>

2页

 <?php 
 session_start();

if(isset($_SESSION['id']))
 {
 $uid = $SESSION['id'];
 $name = $_SESSION['user'];
 $result = " id:".$uid."user:".$name;   
  }
 else
{    $result = "not logged in";   }

?>

2 个答案:

答案 0 :(得分:3)

改为:

from ::
$uid = $SESSION['id'];
To
$uid = $_SESSION['id'];

答案 1 :(得分:0)

你只是让问题复杂化。您是在php代码中初始化登录变量还是从表单输入发送它。 任何方式创建表如下,并尝试使用下面的代码登录。你会没事的。  此代码已经过测试,适用于您

create table users(id int  primary key auto_increment, username varchar(100), password varchar(100));
insert into users(username,password) values('sectona','sectona09');




<?php







$db = new PDO (

    'mysql:host=localhost;dbname=sectona_db;charset=utf8', 

    'root', // username



    'root89' // password

);
?>


<?php
  session_start();

require("pdo.php");


$uname=$_POST["uname"];
$pass=$_POST["pass"];



$statement = $db->prepare('
        SELECT * FROM users
        WHERE username = :username
        AND password = :password 

    ');


    $statement->execute(array(
':username' => $uname,
':password' => $pass));




    if ($row = $statement->fetch()) {
// Ensure that session fixation attack is not possible
session_regenerate_id();

           $_SESSION['SESS_USERNAME'] = $row['username'];


$_SESSION['SESS_PASSWORD'] = $row['password'];


echo '<script>
window.setTimeout(function() {
    window.location.href = "welcome.php";
}, 2000);
</script>';



}else{

echo '<font color=red size=2><b>Wrong Login Account</b></font>';

}





?>

的welcome.php

<?php
session_start();

Welcome <?php echo  $_SESSION['SESS_USERNAME']; ?>

?>