简体   繁体   中英

Session is not storing variables in PHP?

I'm facing dilemma that the session I created to store the varibale(the user name) is not showing. I want to show the user name in the maindash.html after when the user sign up in index.php .

maindash.html code:

<!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>
        MY SITE PLANNER | DASHBOARD
      </title>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"\>
      <link href="custom.css" rel="stylesheet" type="text/css"  />
    </head>
    <body class="formbg">
      <div class="container-fluid pi">
        <div class="row">
            <div class="col-md-6 col-sm-6 col-xs-6">
                <p class="ppp">
                    <img src="image/my-site-planner-logo.png" class="ppp" />
                </p>
            </div>
            <div class="col-md-6 col-sm-6 col-xs-6">
                <p class="up">
            <!--        Hi, <a href="#">user</a> -->
              <a href="#">Hello, <?php echo $_SESSION["name"]; ?></a>
                        <a href="#">settings</a>
                        <a href="#">logout</a>
                </p>
                <p id="dt">
                    System:
                </p>
            </div>
        </div>
      </div>

      <div class="container-fluid fill">
        <div class="row">
          <div class="col-md-12 col col-sm-12 col-xs-12">
            <div class="navbar">
              <ul class="nalink">
                <li>
                  <a href="trans.html">ViewIncomeStatement</a> 
                </li>
                <li>
                  <a href="siteform.html">View/AddSite</a>
                </li>
                <li>
                  <a href="vendor.html">View/AddVendor</a>
                </li>
                <li>
                  <a href="item.html">View/AddItem</a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6 col-sm-6 col-xs-12">
            <h3 class="side">
              Summary of your site 
            </h3>
          </div>
          <div class="col-md-6 col-sm-6 col-xs-12">
            <button class="btn btn-success view" name="submit" type="submit" >
              View Income Statement
            </button>
          </div>
        </div>  
      </div>

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="table-responsive line">
              <table class="table table-bordered dem">
                <thead>
                  <tr class="hd">
                    <th width="3">Sr.</th>
                    <th width="6">Site Name</th>
                    <th width="6">Incomes</th>
                    <th width="6">Expenses</th>
                    <th width="6">Payables</th>
                    <th width="6">Balance</th>
                    <th width="10">Last Week Details</th>
                  </tr>
                </thead>
              <tbody>
                  <tr>
                    <td>1</td>
                      <td><a href="#">SITE A</a> </td>
                      <td><a href="#">200000</a></td>
                      <td><a href="#">110000</a></td>
                      <td><a href="#">36000</a></td>
                      <td><a href="#">79000</a></td>
                      <td>Completed kitchen tiles work</td>
                  </tr>
                  <tr>
                      <td>2</td>
                      <td><a href="#">SITE B</a></td>
                      <td><a href="#">500000</a></td>
                      <td><a href="#">425000</a></td>
                      <td><a href="#">88000</a></td>
                      <td><a href="#">-13000</a></td>
                      <td>completed kitchen tiles work</td>
                  </tr>
                  <tr>
                      <td></td>
                      <td>TOTAL</td>
                      <td>700000</td>
                      <td>555000</td>
                      <td>124000</td>
                      <td>66000</td>
                      <td></td>
                  </tr>
              </tbody>
            </table>
            </div>  
          </div>
        </div>
      </div>

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6 col-sm-6 col-xm-12">
            <h3 class="side">
              Your to do list 
            </h3>
          </div>
          <div class="col-md-6 col-sm-6 col-xm-12">
            <button class="btn btn-success view" name="submit" type="submit">
              Add Item
            </button>
          </div>
        </div>
      </div>

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 col-sm-12 col-xm-12">
            <div class="table-responsive line">
                <table class="table table-bordered dem">
                  <thead>
                    <tr class="hd">
                      <th width="3">Sr.</th>
                      <th width="15">Task</th>
                      <th width="15">Site</th>
                      <th width="15">Added</th>
                      <th width="20">Action</th>
                    </tr>
                  </thead>
                <tbody>
                    <tr>
                      <td>1</td>
                        <td>Need to co-ordinate with shahid bhai for comment bags</td>
                        <td>Site A</td>
                        <td>23-02-2016</td>
                        <td>
                          <a href="#">Delete Task</a>&nbsp;<a href="#">Mark as complete</a>
                        </td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>Get registrations done</td>
                        <td>Site B</td>
                        <td>23-02-2016</td>
                        <td><a href="#">Delete Task</a> &nbsp; <a href="#">Mark as complete</a></td>
                    </tr>
                </tbody>
              </table>
            </div>  
          </div>
        </div>
      </div>  

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6 col-sm-6 col-xm-12">
            <h3 class="side">
              Quick Navigation 
            </h3>
          </div>
        </div>
      </div>  

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 col-sm-12 col-xm-12">
            <div class="table-responsive line">
              <table class="table table-bordered dem">
                <tr class="hd">
                  <th>
                    <span>Select Site</span>
                    <select name="site" class="hig"> 
                      <option value="" selected="selected">Fetch site list</option>
                      <option value="site A">site A</option>
                      <option value="site B">site B</option>
                    </select>
                    <span class="dis">Select Operations</span>
                      <select name="site" class="hig"> 
                      <option value="" selected="selected">select your option</option>
                      <option value="option 1">View Site-wise imcome statement</option>
                      <option value="option 2">View Site-wise vendors</option>
                      <option value="option 3">View Site-wise items list</option>
                      <option value="option 4">List of Vendors Vs Expenses</option>
                      <option value="option 5">List of Item Vs Expenses</option>
                      <option value="option 6">List of Labours Vs Expenses</option>
                      <option value="option 7">List of Service Vs Expenses</option>
                      <option value="option 8">Payables List</option>
                      <option value="option 9">To-do List</option>
                      <option value="option 10">Work Done List</option>
                    </select>
                    <button class="btn btn-success viewl" name="submit" type="submit" >GO</button>
                  </th>
                </tr>
              </table>        
            </div>  
          </div>
        </div>
      </div>


      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6 col-sm-6 col-xm-12">
            <h3 class="side">
              Reporting Definations 
            </h3>
          </div>
        </div>
      </div>  

      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 col-sm-12 col-xm-12">
            <div class="table-responsive line">
              <table class="table table-bordered dem">
                <tbody>
                  <tr class="info">
                    <td>View Site-wise imcome statement</td>
                    <td>Show Income Statement of a particular site</td>
                  </tr>
                  <tr>
                    <td>View Site-wise vendors</td>
                    <td>Show all the vandors of a perticular site</td>
                  </tr>
                  <tr class="info">
                    <td>View Site-wise items list</td>
                    <td>Show all the Items registerd in a particular</td>
                  </tr>
                  <tr>
                    <td>List of Vendors Vs Expenses</td>
                    <td>List of all vandors registerd in a perticular site vs amount paid to them</td>
                  </tr>
                  <tr class="info">
                    <td>List of Item Vs Expenses</td>
                    <td>List of all the Items used/registerd in a perticular site and the amount utilized on them</td>
                  </tr>
                  <tr>
                    <td>List of Labours Vs Expenses</td>
                    <td>List of the labours used in a perticular site and the amount paid to them</td>
                  </tr>
                  <tr class="info">
                    <td>List of Service Vs Expenses</td>
                    <td>List of the Service used in a perticular site and the amount paid to them</td>
                  </tr>
                  <tr>
                    <td>Payables List</td>
                    <td>List of Payables in  perticula site</td>
                  </tr>
                  <tr class="info">
                    <td>To-do List</td>
                    <td>To-do List of a perticular site</td>
                  </tr>
                  <tr>
                    <td>Work Done List</td>
                    <td>List of Works Done on a perticular site</td>
                  </tr>
                  <tr class="info">
                    <td></td>
                    <td></td>
                  </tr>
                </tbody>
              </table>
            </div>  
          </div>
        </div>
      </div>  

      <script>
        document.getElementById("dt").innerHTML = Date();
      </script>

      <script src="https://code.jquery.com/jquery-2.2.0.js"></script>
    </body>
  </html>

index.php code:

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "index";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$email      =   $_POST['email'];
$password   =   $_POST['password'];


$sql = "INSERT INTO form ( email, password )
VALUES ( '$email', '$password')";



                if ($conn->query($sql) === TRUE) {


                header('Location: sign-up.html');

                   } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }

                $conn->close();



if($count == 1)
  {
    $_SESSION['logged'] = true;
    $_SESSION['username'] = $myusername;
    echo "Login worked";
    exit();
  }
else
  {
     $_SESSION['logged']=false;
     echo "Login failed";
     exit();
  }

?>

<li class='active' style='float:right;'>
  <?php 
  if($_SESSION['logged'] == true)
    { 
      echo $_SESSION["name"];
      echo '<a href="logout.php"><span>Logout</span></a></li>';
    }
  elseif($_SESSION['logged'] == false)
    {
      echo '<a href="sign-up.html"><span>Login/Register</span></a></li>';
    }



//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM form where email = $email and password = $password";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo " Name: " . $row["name"]. ".<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
  ?>

Here after the user log in, it doesn't show the user name. I want to make it as the user name should be taken from the database file and displayed in the maindash.html . But it only shows me the Hello as written in my code.

Rename your maindash.html page to have a .php extension. And at the top of the page ( maindash.php ie) ensure you put

<?php
session_start(); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM