Hello everyone, I have 2 of my php pages. I am starting a session in my first page and setting the setting variables but whenever I am trying to access that variable in the 2nd page it doesn't show an error neither it pops out my desired result. In short its not showing the session value, its null. In my 2nd page(cms.php) at line 48 you can see that my variable is called but no output is there please help

Login.php

<?php include_once("../includes/connection.php");?>
<!DOCTYPE html>
<?php
session_start();
 ?>
<html lang='en'>
<head>
    <meta charset="UTF-8" /> 
    <title>
        HTML Document Structure
    </title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div id="wrapper">

<?php
$current_page = $_SERVER['PHP_SELF'];
?>
    <form name="login-form" class="login-form" action= "<?php echo $current_page;?>" method="post">

        <div class="header">
        <h1>Login Form</h1>
        <span>Fill out the form below to login to my super awesome imaginary control panel.</span>
        </div>

        <div class="content">
        <input name="username" type="text" class="input username" placeholder="Username" />
        <div class="user-icon"></div>
        <input name="password" type="password" class="input password" placeholder="Password" />
        <div class="pass-icon"></div>     
        </div>



        <div class="footer">
        <input type="submit" name="button" value="submit" class="button" />
        <a href="../qmc-reg/reg.php" style="color:#000" >  Register</a>
        </div>

    </form>
    <?php
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];

$query_one  = "SELECT * ";
$query_one .= "FROM users ";
$query_one .= "WHERE user_name = '".$username."' ";
$query_one .= "AND user_pass = '".$password."' ";
$query_one .= "LIMIT 1";

$result = mysql_query($query_one) or die(mysql_error());
$count = mysql_num_rows($result);

if ($count == 1){
$something  = mysql_fetch_array($count);
$_SESSION['user_id'] = $something1;
$_SESSION['user_name'] = $something;
header("location: ../cms/cms.php");
}
else{
    echo "<div> Invalid Credentials </div>";
}
}
?>

</div>
<div class="gradient"></div>

</body>
</html>

CMS.php

<?php 
session_start();
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>QMC Home</title>
</head>

<body>
<?php
$connection = mysql_connect('localhost','root','');
if(!$connection){
die("Database Connection Failed". mysql_error());
}
$select_db = mysql_select_db('hamdard_attendance');
if(!$select_db){
die("Database Connection Failed" . mysql_error());
}
$something = isset($_POST['user_name'])?$_POST['user_name']:'';
if($something){
$_SESSION['user_name']=$something;
}
?>
<div id="container">
        <div id="header">
            <h1 style="text-align:left">Quality Management<span class="off"> Cell</span></h1>

        </div>   

        <div id="menu">
            <ul>
                <li class="menuitem"><a href="cms.php">Home</a></li>
                <li class="menuitem"><a href="cms-attendance.php">Attendance</a></li>
                <li class="menuitem"><a href="cms-courses.php">Courses</a></li>
                <li class="menuitem"><a href="cms-settings.php">Settings</a></li>

            </ul>
            <a style="text-align:right" href="cms-logout.php">Logout</a>
        </div>

        </div>        
        <div id="content">
        <div id="content_top"></div>
        <div id="content_main">
<p> Welcome & <?php echo "Hello, ".$something."";?> </p>
<?php                                
$query_three = "SELECT s.stdnt_name, c.course_name FROM students s inner JOIN student_courses sc ON sc.student_id = s.stdnt_rfid_tag INNER JOIN users u ON s.stdnt_name = u.name INNER JOIN courses c ON c.course_id = sc.course_id where u.user_id = '".$something."'"; 
$result_attendance3 = mysql_query($query_three) or die(mysql_error());
echo "<table border='1': border-color: silver;'>";
echo "<tr>";
echo "<td align='center' width='200'>" . "<h4>"."Student Name" ."</h4>". "</td>";
echo "<td align='center' width='200'>". "<h4>"."Course Name" ."</h4>". "</td>";
echo "</tr>";
echo "</table>";
while($row = mysql_fetch_array($result_attendance3)){
echo "<br />";
//echo "<td align='center' width='200'>".$row['st_classes_attempt'] . "</td>";
echo "<table border='1': border-color: silver;'>";
echo "<tr>";
echo "<td align='center' width='200'>".$row['stdnt_name'] . "</td>";
echo "<td align='center' width='200'>".$row['course_name'] . "</td>";
//echo "<td align='center' width='200'>".$row['st_classes_attempt'] . "</td>";
echo "</tr>";
echo "</table>";
} 
?>

            <p>&nbsp;</p>
            <p>&nbsp;</p>

        <div id="content_bottom"></div>


      </div>
   </div>
</body>
</html>

Declaration of session_start() must be in the First line of code.

Still containing the same problem :(

It looks like this logic might be a little askew in cms.php:

$something = isset($_POST['user_name'])?$_POST['user_name']:'';
if($something) {
    $_SESSION['user_name']=$something;
}

As you are attempting to pull it out of the session in cms.php after the login, it should be

$something = isset($_SESSION['user_name']) ? $_SESSION['user_name'] : null;
if ($something === null) {
    echo '<p>Not logged in.</p>';
    //exit();
}

(Just for reference, this is a continuation )

Dear faisal your trick worked :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.