Domanda

I have a school project where I'm trying to use session variables to save a log in ID, however I can't seem to access the session variables across files. I'm relatively new to sessions, but have gotten them to work in simpler applications on the same server that I am using for this project. The program works as follows:

getData.php is the log in form where the user enters their ID and password. front.php collects data from this form and forwards it to another server (of another group member) using php curl. Since this is not my part, I do not have this code. After accessing a database, information from the database is forwarded back to me, also using curl and I format it in a table, in tList.php.

The problem is that the session variables do not seem to be accessible between the files on MY SERVER. The exact goal I am trying to accomplish is to access the session variables that I create in front.php on tList.php, however the variables are not set even though I assign them in front.php.

Any help would be greatly appreciated. Thank you.

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>

Here is the working application:

file1:

<?php
session_start();

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

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

?>

file2:

<?php
session_start();

echo $_SESSION['test'];

?>

Nessuna soluzione corretta

Altri suggerimenti

The session_start() function must appear BEFORE the tag

<?php session_start(); ?>

<html>
<body>

</body>
</html> 

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

Try this to make sure you are passing valid values into the SESSION variable.

 <?php
    session_start();
    if (!empty($_POST["txtUCID"])){
      $_SESSION['id'] = $_POST["txtUCID"];
    } else {
    $_SESSION['id'] = 'no value set';
    }
echo $_SESSION['id'];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top