help me in php session

Discussion in 'PHP' started by nadhim, Aug 10, 2012.

  1. #1
    hello i really need help
    i cant get session to work in a particular php file but it works with others here are the codes


    i started with login.php

    login.php
    <?php
    
     
    include("config.php");
    
    
     session_start();
     if($_SERVER["REQUEST_METHOD"] == "POST")
     {
     
    // username and password sent from Form 
    
    
    $myusername=addslashes($_POST['username']); 
    $mypassword=addslashes($_POST['password']); 
    
    
    $sql="SELECT id FROM users WHERE username='$myusername' and password='$mypassword'";
     $result=mysql_query($sql);
     $row=mysql_fetch_array($result);
     $active=$row['active'];
     $count=mysql_num_rows($result);
     
    
    
    
    
    // If result matched $myusername and $mypassword, table row must be 1 row
     
    if($count==1)
     {
     session_register("myusername");
     $_SESSION['login_user']=$myusername;
     
    
    
    header("location: users/panel.php");
     }
     else 
    {
    header("Location: invalid_login.html");
     }
     }
     
    ?>
    
    
    
    Code (markup):
    then this will start session in

    lock.php

    <?php 
    include('config.php');
     session_start();
     
     $user_check=$_SESSION['login_user'];
     
    $ses_sql=mysql_query("select username from users where username='$user_check' ");
     
    $row=mysql_fetch_array($ses_sql);
     
    $login_session=$row['username'];
    
    
    
    
    
    
    if(!isset($login_session))
     {
         
     header("Location: login.php");
     }
    
    
    ?>
    Code (markup):

    then the result will be showed in auth.php

    <?php
    
    
    include('lock.php');
     
     print $login_session; 
     
     
     ?>
    
    Code (markup):


    this result will be shown in the index page
    i have a table with marks and i need to do chart and show marks

    to show marks i used the code

    get_marks.php and thin include it in students page to see results



    
    
    
    <?php
    
    
    
    
    include("config.php"); 
    
    
    // connect to the mysql server 
    $link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
    or die ("Could not connect to mysql because ".mysql_error()); 
    
    
    // select the database 
    mysql_select_db($mysql_database) 
    or die ("Could not select database because ".mysql_error()); 
    
    
    
    
    $query  = "SELECT id, sno, maths, english, physics, servicew, ast, defstudy, nav, other FROM Marks WHERE sno = '$login_session'";
    $result = mysql_query($query);
    
    
    
    
    if (mysql_query($query)) {
        echo "";
    } else {
        echo 'Incorrect Name: ' . mysql_error() . "\n";
    }
    
    
    echo "<table border='0' bordercolor='#ffffff'  width=200 cellspacing=10 CELLSPACING=0 padding-bottom: 10px style='color:#629fa8 ;font-size:15px ;font-family: Georgia; font-weight:blod; background-color:#ffffff'>
     <tr>
    
    
     </tr>";
     
    while($row = mysql_fetch_array($result))
       {
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: Georgia; border-bottom:non; font-weight:normal; background-color:#ffffff'>English:</td>";
    
    
    
    
       echo "<td>" . $row['english']. "</td>";
    echo "</tr>";
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Mathmatics:</td>";
       echo "<td>" . $row['maths'] . "</td>";
    echo "</tr>";
    
    
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Physics:</td>";
       echo "<td>" . $row['physics'] . "</td>";
    echo "</tr>";
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Servics writing:</td>";
       echo "<td>" . $row['servicew'] . "</td>";
    echo "</tr>";
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>AST:</td>";
       echo "<td>" . $row['ast'] . "</td>";
    echo "</tr>";
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Navigation:</td>";
       echo "<td>" . $row['nav'] . "</td>";
    echo "</tr>";
    
    
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Defence Studies:</td>";
       echo "<td>" . $row['defstudy'] . "</td>";
    echo "</tr>";
    
    
    
    
       echo "<tr>";
    echo "<td style='color:#2f2f2f; font-size:15px; font-family: arial; font-weight:normal; background-color:#ffffff'>Other:</td>";
       echo "<td>" . $row['other'] . "</td>";
    echo "</tr>";
    
    
    
    
    
    
    
    
       }
     echo "</table>";
     
    
    
    ?>
    Code (markup):






    notice this

    $query  = "SELECT id, sno, maths, english, physics, servicew, ast, defstudy, nav, other FROM Marks WHERE sno = '$login_session'";
    Code (markup):
    i used the $login_session to specify the logged student number and get his marks from the table
    this works fine and the marks showed for everybody logged in by taking his username( which is his number ) and get the data from MYSQL


    but the chart has problems why ?
    i have no idea

    the chart includes


    phpgraphlib.php to configure it

    and mysql_graph_bar.php

    <?php
    
    
    include("phpgraphlib.php");
    $graph=new PHPGraphLib(320,170); 
    
    
    include("config.php"); 
    
    
    // connect to the mysql server 
    $link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
    or die ("Could not connect to mysql because ".mysql_error()); 
    
    
    // select the database 
    mysql_select_db($mysql_database) 
    or die ("Could not select database because ".mysql_error()); 
    
    
    
    
    $query  = "SELECT id, sno, maths, english, physics, servicew, ast, defstudy, nav, other FROM marks WHERE sno = '$login_session'";
    
    
    
    
    $result = mysql_query($query); if ($result) {  
    while ($row = mysql_fetch_assoc($result)) {  
    
    
        
    $english=$row["english"];
    $maths=$row["maths"];
    $physics=$row["physics"];
    $servicew=$row["servicew"];
    
    
    $ast=$row["ast"];
    $nav=$row["nav"];
    $defstudy=$row["defstudy"];
    $other=$row["other"];
    
    
    
    
    
    
         
    //add to data areray      
    $dataArray[$english]=$english; 
    $dataArray[$maths]=$maths; 
    $dataArray[$physics]=$physics; 
    $dataArray[$servicew]=$servicew; 
    
    
    $dataArray[$ast]=$ast; 
    $dataArray[$nav]=$nav; 
    $dataArray[$defstudy]=$defstudy; 
    $dataArray[$other]=$other;
    
    
     }
    
    
    }  
    //configure graph
    $graph->addData($dataArray);
    $graph->setupXAxis(12, 'gray');
    $graph->setupYAxis(12, 'gray');
    $graph->setTextColor('gray');
    $graph->setGridColor('236,236,236');
    $graph->setDataValues(false);
    $graph->setGoalLine('65');
    $graph->setGoalLineColor('red');
    $graph->setDataFormat('%');
    $graph->setGradient("Teal", "Teal");
    $graph->setBarOutlineColor("gray");
    $graph->setDataValues(false);
    $graph->createGraph();
    
    
    
    Code (markup):
    and this will be showed as an image in the student page by writing this code in the students page

    <img src="mysql_graph_bar.php" />
    Code (markup):


    the problem when i put the session same as marks after (sno) i wont get anything it fails and give damaged image where the chart should be

    Example: this doesn't work because i used sno = '$login_session'

    $query  = "SELECT id, sno, maths, english, physics, servicew, ast, defstudy, nav, other FROM marks WHERE sno = '$login_session'";
    Code (markup):
    when i changed the value to a specific student number (sno) from the table (marks) it works

    example student number 123456

    $query  = "SELECT id, sno, maths, english, physics, servicew, ast, defstudy, nav, other FROM marks WHERE sno = '123456'";
    
    Code (markup):


    please help me to get the session result works..
     
    nadhim, Aug 10, 2012 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    session_start();

    ..needs to be directly under:

    <?php

    ie:

    
    <?php
    session_start();
    
    Code (markup):
     
    MrLeN, Aug 13, 2012 IP
  3. mike30

    mike30 Well-Known Member

    Messages:
    887
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    140
    #3
    replace this:
    
    '$login_session'
    PHP:
    for this:
    
    \'$login_session\'
    PHP:
    this should fix the problem ;)

    ~Mike
     
    mike30, Aug 15, 2012 IP
  4. SitesTen

    SitesTen Active Member

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    75
    #4
    just place session_start() at the beginning. You don't actually need to use session_register().
     
    SitesTen, Aug 16, 2012 IP
  5. lemonsquad

    lemonsquad Greenhorn Affiliate Manager

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Dead on. No need for session_register.
     
    lemonsquad, Aug 21, 2012 IP