在多个文件中访问PHP会话变量

时间:2014-03-03 19:38:34

标签: php session curl

我有一个学校项目,我正在尝试使用会话变量来保存日志ID,但是我似乎无法跨文件访问会话变量。我对会话比较陌生,但是让他们在我用于这个项目的同一台服务器上的更简单的应用程序中工作。该计划的工作原理如下:

getData.php是用户输入其ID和密码的登录表单。 front.php从这个表单收集数据并使用php curl将其转发到另一个服务器(另一个组成员)。由于这不是我的职责,我没有这个代码。 访问数据库之后,数据库中的信息也会转发回给我,也使用curl并将其格式化为tList.php中的表格。

问题是MY SERVER上的文件之间似乎无法访问会话变量。我想要完成的确切目标是访问我在tList.php上的front.php中创建的会话变量,但是即使我在front.php中分配它们,也不会设置变量。

非常感谢任何帮助。谢谢。

访问getdata.php:

<?php

session_start();

?>

<html>
<head>

    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <link rel="stylesheet" type="text/css" href="../css/b.css">
    <link href="../css/test.css" rel="stylesheet" type="text/css">

</head>

<body>

<script type="text/javascript">

    function status_toggle() {

        if(document.getElementById('stud').checked) {

            document.getElementById('sl').style.display='block';
            document.getElementById('tl').style.display='none';

        } else if(document.getElementById('teach').checked) {

            document.getElementById('sl').style.display='none';
            document.getElementById('tl').style.display='block';

        }
    }

</script>

<!-- Status Selection -->

<form name="radio" class="nav selection_form" action="" align="center">

    <div align="center" class="">
        Are you a <br>
        Student <input id="stud" type="radio" name="status" value="student" onclick="javascript:status_toggle();" checked>
        &nbsp&nbsp&nbsp or &nbsp&nbsp&nbsp
        Teacher <input id="teach" type="radio" name="status" value="teacher" onclick="javascript:status_toggle();">
    </div>

</form>

<!-- Student Form -->

<form id="sl" name="student_login" action="front.php" class="input-block my_form" autocomplete="on" method="post">

    <div align="center">
        Username: <input type="text" name="txtUCID" size="15" /><br />
        Password: <input type="password" name="txtPasswd" size="15" /><br />
            <p><input class="btn-red" id="submit" type="submit"  value="Login" /></p>
    </div>

</form>

<form style="display:none" id="tl" name="teacher_login" action="front.php" class="input-block my_form" autocomplete="on" method="post">

    <div align="center">
        Username: <input type="text" name="teacherTxtUCID" size="15" /><br />
        Password: <input type="password" name="teacherTxtPasswd" size="15" /><br />
            <p><input class="btn-grn" id="submit" type="submit"  value="Login" /></p>
    </div>

</form>

<div align="center" class="alert_box input-block my_form">

        <h2 align="center" type="text" id="local_db"> change me!</h2>
        <h2 align="center" type="text" id="njit_db"> change me!</h2>

</div>

</body>

</html>

front.php:

<?php
session_start();

$un = $_POST["txtUCID"];
$pw = $_POST["txtPasswd"];

$_SESSION['id'] = $un;

$curl = curl_init('web.njit.edu/~mc332/cs_490/loginSTD.php');

$postData= array(

    'txtUCID' => $un,
    'txtPasswd' => $pw

);

curl_setopt_array($curl, array(

    CURLOPT_RETURNTRANSFER => 0,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_MAXREDIRS => 5

));

echo $_SESSION['id'];

$result = curl_exec ($curl);

curl_close ($curl);

tList.php

<?php
session_start();
?>

<html>

<head>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <link rel="stylesheet" type="text/css" href="list.css">
</head>

<body>

<div class="header">
    Your Tests
</div>

<div class="list-view">

    <?php
        echo "SV: ".$_SESSION['id'];

        $result = $_POST['tests'];

        echo "<table>";
        echo "<tr><th>Test ID</th><th>Test Name</th><th>Attempted</th><th>Score</th><th> Attempt Test </th></tr>";

        $json = json_decode($result);

        $i = 0;
        while($i < count($json)) {

            $id = $json[$i][0];
            $name = $json[$i][1];

            $tId = $id;

            echo "<tr> <td> $id </td> <td> $name </td> <td> ? </td> <td> ?/100 </td> <td> <form method=\"post\" action=\"goToTest.php\" id=\"form$tId\"><button type=\"submit\">Take Test</button></input><input type=\"text\" style=\"display: none;\" name=\"poop\" value=\"$tId\"></input></form></td> </tr>";

            $i++;
        }

        echo "</table>";

        echo "end";

    ?>

</div>

</body>

</html>

以下是工作申请表:

文件1:

<?php
session_start();

$_SESSION['test'] = "test";

echo "<a href=\"t2.php\">Click</a>";

?>

file2的:

<?php
session_start();

echo $_SESSION['test'];

?>

1 个答案:

答案 0 :(得分:1)

session_start()函数必须出现在标记

之前
<?php session_start(); ?>

<html>
<body>

</body>
</html> 

http://www.w3schools.com/php/php_sessions.asp

尝试此操作以确保将有效值传递给SESSION变量。

 <?php
    session_start();
    if (!empty($_POST["txtUCID"])){
      $_SESSION['id'] = $_POST["txtUCID"];
    } else {
    $_SESSION['id'] = 'no value set';
    }
echo $_SESSION['id'];