使用第一个中的变量重定向到另一个页面

时间:2014-03-07 08:02:17

标签: php session session-cookies

我创建了以下场景。

  1. 我有index.php文件,显示主页。在此,有两个字段 - 用户ID和密码包含在表单标记中。提交按钮调用login.php文件。

  2. Login.php验证用户ID,密码等

  3. 验证成功后,我希望login.php页面带我到MyDashboard.php页面(传递用户ID和密码)。

    我在PHP中尝试过Header但是没有用。我还试图做一个Javascript window.location.href并尝试在$(document).ready上调用它,但没有任何反应。

    请帮忙。

  4. ---编辑---- 这是修改后的代码

       <?php
          include_once('./library/Common.php');
          $_EmailId = trim($_POST['validemailid']);
          $_Password = trim($_POST['password1']);
          $_Rememberme = trim($_POST['rememberme']);
    
          // Get the username from the Email Id by searching for @
    
          $_UName= substr($_EmailId, 0, strpos($_EmailId, '@'));
    
          $_Password = md5($_Password);
    
          session_start();
    
          $_SESSION['username'] = $_UName;
    
    
    
          $query = "select username, firstname, password_hash,userstatus from users where username = ? and emailid = ?";
    
    
          $dbconn = new mysqli('localhost', 'root', '','myDB');
    
          if($dbconn->connect_errno)
          {
            print getHTML('ERROR', "Error in connecting to mysql".$dbconn->connect_error);
          }
    
          if(!($stmt=$dbconn->prepare($query)))
          {
            print getHTML('ERROR',"error in preparing sql statement".$dbconn->error);
          }
    
          if(!($stmt->bind_param('ss',$_UName,$_EmailId)))
          {
            print getHTML('ERROR',"error in binding params in sql statement".$stmt->error);
          }
    
          if(!$stmt->execute())
          {
            print getHTML('ERROR',"Execute failed: (" . $stmt->errno . ") " . $stmt->error);
          }
    
          $result=$stmt->get_result();
          $row = $result->fetch_assoc();
    
          $_dbpwd = $row['password_hash'];
          $_userstatus = $row['userstatus'];
    
          $errstatus = false;
    
    
          if ($row['username'] != $_UName)
          {
            print getHTML('ERROR',"User does not exist with the given email id: ".$_EmailId);
    
            $errstatus = true;
          }   
    
          if(($row['password_hash'] != $_Password) && !$errstatus)
          {
            print getHTML('ERROR',"Password does not match");
            $errstatus = true;          
          }
    
          if(($row['userstatus'] != 'ACTIVE') && !$errstatus)
          {
            print getHTML('ERROR',"User is inactive. Please check your email for activation");
            $errstatus = true;
          }
    
          if(!$errstatus)
          {
            $_SESSION['firstname'] = $row['firstname'];
            $chksession = "SELECT sessionid FROM USERSESSIONS WHERE USERNAME = ? AND ENDDATE IS NULL";
    
            if(!($sessionstmt=$dbconn->prepare($chksession)))
            {
                print "error in preparing sql statement".$dbconn->error;
                exit();
            }
    
    
            $sessionstmt->bind_param('s',$_UName);
    
            $sessionstmt->execute();
    
    
            $sessionresult=$sessionstmt->get_result();
    
            $sessionrow= $sessionresult->fetch_assoc();
    
            $currdate = date('y-m-d H:i:s');
    
    
    
    
            if($sessionrow['sessionid'] == 0)
            {
                $insertstmt = $dbconn->query("INSERT INTO USERSESSIONS(USERNAME,STARTDATE,ENDDATE) VALUES ('".$_UName."','".$currdate."',null)");
    
                $insertstmt->close();
            } 
          }
    
          $sessionstmt->close();
          $stmt->close();
          $dbconn->close();
    
          header("Location :MyDashboard.php");
          exit;
    
              ?>
          --- End of Edit -----
    

    阿米特

5 个答案:

答案 0 :(得分:3)

您应该使用会话变量在登录会话中存储变量。不建议也不要将密码传递给其他页面。阅读Sessions,并查看已有的登录脚本。下面是非常简单的示例,使用header() function重定向到下一页。

<?php

// Validate user credentials and save to session
session_start();
$_SESSION['userId'] = $userId;

// Redirect to next page
header("Location: dashboard.php");

// Make sure that code below does not get executed when we redirect
exit;

?>

答案 1 :(得分:2)

如果用户通过身份验证, 在PHP中:

header('Location:MyDashboard.php');

答案 2 :(得分:0)

尝试include()

此功能允许您包含来自其他PHP脚本的代码。

答案 3 :(得分:0)

标题功能是正确的方法。只要在调用头函数之前没有任何输出,它就应该有效。

http://us3.php.net/manual/en/function.header.php

发布你的代码,让我们看看它是什么不起作用!

答案 4 :(得分:0)

标题应该适合你的情况。

您可以使用以下代码:

header("Location:filename");
exit();