0

This is my code:

<?php
 ob_start();
 session_start();
 include("index.php");
 if (isset($_POST['user'],$_POST['pass'])):
     $con=mysqli_connect("connect info");
     if ( !$con ){
         die('Could not connect to Database: '.mysqli_error());}
 $pass=($_POST['pass']);
 $query0 = "SELECT * FROM user WHERE username = '" . $_POST['user'] ."' AND password = '" . $pass . "'";
 $resource0 = mysqli_query($con,$query0);
 if(!$resource0):
     die("Error conducting query. ".mysqli_error($con));
  endif;
  if(mysqli_num_rows($resource0) == 0){
        echo "Username not find";
        header("Location: /login.php");}

   $result0 = mysqli_fetch_row($resource0);
       $_SESSION['ID'] = $result0[0];
       $_SESSION['userType'] = $result0[3];

    if(!isset($_SESSION['ID'])):
        //header("Location: ...");
    else:
        header("Location: ....");
    endif;
else: 
    if(isset($_POST['from'])):
        $_SESSION['from'] = $_POST['from'];
    endif;

    ?>

<?php endif; ?>

This code takes a user's username and password information and stores and searching the database for the userID. Once is finds the information it stores in the session variable 'ID'.

PROBLEM: When the session 'ID' variable is passed to the next page it is not set. Surprisingly, this code without the ob_start function was working yesterday morning but wasn't working by the afternoon. I know it's not setting because two things: When I echo on the next page, nothing appears and because when I try to run a query with the session 'ID' variable I get a mysql error saying the query could not be conducted.

This is the code on the next page that is not working. At first I thought, it might be something wrong with my query because I was getting this error:

"Error conducting query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

But when I tried printing the session 'ID' variable nothing is printed.

<?php session_start;
  // Create connection
  $con=mysqli_connect(connect info....);

 // Check connection
 if (mysqli_connect_errno($con))
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $query0 = "SELECT * FROM studentProfile WHERE sID = ".$_SESSION['ID'];
 $resource0 = mysqli_query($con,$query0);
 if(!$resource0):
echo $_SESSION['ID'];
die("Error conducting query. ".mysqli_error($con)); 
 endif;

 .......(rest of code)

Solutions Already Tried: I have tried them all...session_write_close(), session_regenerate_id(true), session_commit(), ob_end_flush(). I have tried initially setting the session variable on the home page but once the functions are performed in the session 'ID' variable is no longer set at all.

Please help! I have read all the forums I could find on this problem but nothing seems to work.

1 Answers1

0

what you have written on the next page that your code is not working. Please provide me with detail. try this on the page which you have directed by header location. May be your session can work.

<?php
   include('config.php');

   if(!isset($_SESSION['ID']))
   {
     header("Location: ../index.html");
   }
  else{
     $user_id=$_SESSION['ID'][0];
     $user_name=$_SESSION['ID'][1];
?>

and end the else part at the last of the page

Php developer
  • 446
  • 4
  • 18
  • I updated the question with the information. I also tried the code that you suggested. I added it the Profile page. However, since the session ID is not being set on the index page I just get redirected back to the index page. – user2510805 Jun 22 '13 at 11:29