Need help with PHP session

I am working on a PHP page which can be reached through logging in another index page. This PHP page then starts a session and it displays data from MySQL onto a table.

I am trying to add a filter dropdown box, which can query certain data form table depending on the item selected. This requires the page to get refreshed and come up with certain data from the table.

But once I select an option, the PHP page goes white and nothing comes up. But when I goto this page straightaway without logging in, and by commenting out the session commands like “session_start()” and all, the page can display the data based upon the selection in the dropdown box.

So can anyone tell me how to stop this issue, or if you think there is something wrong with the way I found out the cause of the issue, then please notify that as well. Please let me know if you need more details.

Can you paste your source code?

After logging in through index page, the user is shown a page with dropdown box having three items. Based on the selection the appropriate PHP page is displayed. This page that shows the dropdown box for selection has the code in short as follows:


if ($selected_view == 'A')
    include 'A.php';
else if ($selected_view == 'B')
    include 'B.php';
else if($selected_view == 'C')
    include 'C.php';

I am currently working on the C one, so when I select the C it goes to the PHP page having the following code in short:


<?php
session_start();
if ( $_SESSION['usertype'] == 'Admin')
//the following variables hold the firstname and lastname of the logged in user. This can be used to append with the notes the user adds
{
$user = $_SESSION['firstname']." ".$_SESSION['lastname'];

My code that I added for adding the dropdown box is as follows: Note-> this code is a simple one and it worked when I created a sample PHP page and ran it:


<script language="JavaScript" type="text/JavaScript">
function formSubmit()
{
document.getElementById("report_filter").submit();
}
</script>

<form method="post" id="report_filter" action="" >
    <select name="try" onchange="formSubmit();">
    <option value="all">           All         </option>
    <option value="Windows">       Windows     </option>
    <option value="Win 2008">      Win 2008    </option>
    <option value="Win 7">         Win 7       </option>
    <option value="Win Vista">     Win Vista   </option>
    <option value ="Linux">        Linux       </option>
    <option value = "Win 7/Linux"> Win 7/Linux </option>
    </select>
</form>

Since I am in the beginning stage, I am just echoing the option that I select in the dropdown box as follows:


<?php

// I just echo "none" in the beginning, and after a selection is made in te dropdown it should display "ok <the item selected>"

if(!(isset($_POST["try"])))
     echo "none";
else
     echo "ok ".$_POST["try"];

But when I make a selection the page goes white and blank.

So I commented out the “session_start();” and other session related commands I saw and ran this page by directly going to it in the browser, instead of navigating through the index page.

In that case, when I make a selection in the dropdown the “ok <item>” is getting displayed.

Please let me know if you need more information/code.

Can you go to this site and paste the entire contents of each file used here: http://pastebin.com/

Then post the link back here? Remember to select php syntax highlighting.

Are any errors being recorded in PHP’s error log?

session-start() should be the very first line in index.php

Hi,

The index.html is in the link:

In index.html the login happens, and then this leads to login_authentication.php whose code is in the link:

This then leads to admin_select.php whose code is in:

http://pastebin.com/xdt0wvP4

After this the nav_admin.php comes and its code is in link:

http://pastebin.com/qY91DxzW

Then finally the code I am working on comes up, and its in the link:

http://pastebin.com/QvDrgLWx

Please let me know if you need any more info.

Thanks

I dont know exactly how to see the error log, can you please tell me how to see it

session-start() is in the start of the page I am working on. But I have pasted all my code in the links as I mentioned in a comment here. Can you tell me in which code the “session-start()” should be there?

Try renaming your “index.html” file to “index.php” then put these lines at the start of the file:

// index.php


<?php /* index.php */
  session_start();
  error_reporting(-1);
  ini_set('display_errors',1);
  ini_set('error_log', '.\\__________ERROR_LOG.PHP');
  include 'THIS-IS-A-TEST-TO-SEE-IF-THE-_____ERROR_LOG_____.PHP IS WORKING';
?>

Use “http://php.net/manual/en/index.php” to "search for " each function.

Use “http://validator.w3.org/” to validate your files.


Please note that the doctype is your friend and guide to minimise browser conflicts.
All browsers are coded differently but all follow the w3.org standards. BIG problems arise when the errors and warnings occur because the coding team does not have standards to follow. No doubt heated discussions have eventually decided on how the coder’s browser should handle the discrepancies. Also no doubt a time consuming long series of tests also occur to guess a best match.

Thanks for the info. But the code that I am working on is actually “admin_c1.php”. That is where I am having problem with session.

The index.html is the first page that will appear as soon as the user types the website address.

So I just want to know if you are telling me to add these lines for error in “index.html” or “admin_c1.php”?

I included the script at the top of “admin_c1.php” and the following errors were shown:

// admin_c1.php




Parse error: syntax error, unexpected '*' in E:\\xamp-lite\\xampp\\htdocs\\godaddy\\ci_john\\downloads\\sp-a\\learner101\\admin_c1.php on line 771


The faulty script:


 // line 769
     $host="***";
     $username=****;
     $password=****;
     $database=****;
     $connection=mysql_connect(***,***<***); 
     mysql_connect($host,$username,$password);
     @mysql_select_db($database) or die( "Unable to select database");
// line 777


Once you have rectified the script test the MySQL connection success using: mysql_error()

Is this showing error in the mysql connections? I actually put ‘*’ in the hostname, username and password, since I was running this in my localhost. If you really want to test it you can include the sql credentials of a sql db which is in your laptop

OK I tested the script and had to modify to remove errors, etc, so made the following changes:


// remmed the following lines at the bottom of the script because easier to test then exit; on failure
/*  
# < ?php
# } // line 984 approximately
# else
# echo "You are not authorized to use this website. Try to obtain the necessary rights to use it!";
*/

// added these lines at the top of the script
<?php /* admin_c1.php */
	session_start();
	error_reporting(-1);
	ini_set('display_errors',1);
	ini_set('error_log', '.\\__________ERROR_LOG.PHP');
	
	// include 'THIS-IS-A-TEST-TO-SEE-IF-THE-_____ERROR_LOG_____.PHP IS WORKING';

  // the following variables hold the firstname and lastname of the logged in user. 
  // This can be used to append with the notes the user adds
  // $_SESSION['usertype'] 	= 'admin';
  // $_SESSION['firstname']	= 'John';
  // $_SESSION['lastname']	= 'Betong';

  if ( ! isset($_SESSION['usertype']) )
  {
    echo "You are not authorized to use this website. Try to obtain the necessary rights to use it!";
    exit;
  }	
  $user = $_SESSION['firstname']." ".$_SESSION['lastname'];
  require 'connect_sql.php'; 
  $recent_ID_query = mysql_query("SELECT MAX(ID) FROM c1_report");
  ...
  ...
  ...

I did as you said but I couldnt find out the error log file.

The following is the code I used as you mentioned:


<?php

  session_start();
  display_errors = On;
  error_reporting(E_ALL);
  ini_set('display_errors',1);
  ini_set("log_errors", 1);
  ini_set('error_log', dirname(__FILE__) .'\\ERROR_LOG.PHP');

if ( ! isset($_SESSION['usertype']) =='LG Admin') 
  { 
    echo "You are not authorized to use this website. Try to obtain the necessary rights to use it!"; 
    exit; 
  }     

//session_start();
//if ( $_SESSION['usertype'] == 'LG Admin')
//if(true)
//the following variables hold the firstname and lastname of the logged in user. This can be used to append with the notes the user adds
//{
$user = $_SESSION['firstname']." ".$_SESSION['lastname'];
require 'connect_sql.php'; 
$recent_ID_query = mysql_query("SELECT MAX(ID) FROM centrify_report");
..

I then went to php.ini and I removed the ‘;’ in front of the lines:


 display_errors
   Default Value: On
   Development Value: On
   Production Value: On

 error_reporting
   Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
   Development Value: E_ALL
   Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

 log_errors
   Default Value: On
   Development Value: On
   Production Value: On

 track_errors
   Default Value: On
   Development Value: On
   Production Value: On

error_log = php_errors.log

Can you tell me how to generate the error log?

Thanks

@learner101;

Please learn how to wrap your scripts in PHP brackets.

In order to get your error_log file working try this:

// file “Test.php”


<?php /* admin_TEST.php */
  echo __FILE__;

// PREVENT ERRORS FROM SHOWING A BLANK SCREEN
  
/* # gradually move this line down until errors appear 
  
  require '_function_show_setup.php';
  _show_setup();

*/  

die;


// file: “'_function_show_setup.php”


<?php /* _function_show_setup.php */ 
function _show_setup()
{
  defined('jj') ?: define('jj','<br />'); # John's shortcut 

  $xx = 'background-color:#ee0; color:#900; padding:1em; border:dotted 4px #f00';
  echo "<dir style='$xx'>";
      echo jj, 
        '<b>function __get_setup()</b>', jj;
    
      echo jj, 
        isset($_SESSION) ? 'SESSION ALREADY STARTED' : session_start();
      echo jj,
        __DIR__; 
      echo jj,
        __FILE__; 
      echo jj, 
        error_reporting(E_ALL);
      echo jj, 
        ini_set('display_errors',1);
      echo jj, 
        ini_set("log_errors", 1);
      echo jj, 
        $error_log = ini_set('error_log', dirname(__FILE__) .'\\ERROR_LOG.PHP');
      echo jj, 
        $error_log = ini_set('error_log', dirname(__FILE__) .'\\ERROR_LOG.PHP');
      
      include 'asdf.php';
      
      $xx = 'background-color:#eee; color:#090; padding:1em; border:dotted 4px #f00';
      echo "<pre style='$xx'>";
        highlight_file($error_log);
      echo '</pre>';
  echo '</div>';    
}

I did the following but I think I dont fully understand your advice here. Please correct me if what I did is wrong.

I added the code:

require ‘_function_show_setup.php’;
_show_setup();
die;

under the code:

<form method=“post” id=“report_filter” action=“” >
<select name=“try” onchange=“formSubmit();”>
<option value=“all”> All </option>
<option value=“Windows”> Windows </option>
<option value=“Win 2008”> Win 2008 </option>
<option value=“Win 7”> Win 7 </option>
<option value=“Win Vista”> Win Vista </option>
<option value =“Linux”> Linux </option>
<option value = “Win 7/Linux”> Win 7/Linux </option>
</select>
</form>
</td>
</tr>

<?php

if(!(isset($_POST[“try”])))
echo “none”;
else
echo "ok ".$_POST[“try”];

?>
When I select the combo box option the page goes white instead of echoing the option selected, so I thought this is where I am supposed to paste the code. Is this correct?

After doing that I saw the yellow box appearing in my page as the picture I have attached here. Please let me know what I did wrong here.

Thanks

@learner101 ;

Please learn how to wrap your scripts in PHP brackets. To start enclose PHP in square brackets, paste your code then close the script with /PHP inside square brackets.

I find difficulty in trying to see where the file starts and finishes, also the filename and does it start with the <?PHP

Please supply the complete file or a link to the file and I will try to get it to remove errors by uploading the file to my site.

*** If programming was easy then everybody would be doing it :slight_smile:

The index.html is in the link:

In index.html the login happens, and then this leads to login_authentication.php whose code is in the link:

This then leads to admin_select.php whose code is in:

http://pastebin.com/xdt0wvP4

After this the nav_admin.php comes and its code is in link:

http://pastebin.com/qY91DxzW

I then used the code that you gave for showing the error with the same name as _function_show_setup.php which can be found in the link:

http://pastebin.com/kLYnD1KE

Finally comes the code that I have been working on.This will be huge with many different CSS and javascript functions that my team members have worked on. They all work fine and I dont think you have to care about them. You can see the part I was trying to work on from the line 751, where I added a dropdown box.

You can then see the line 772, where I am trying to echo “none” or the option that I select in the dropdown box. You can then see that I included the code you gave me to show the error in the lines 777-779.

This code is called “admin_c1.php” and can be found in the link:

http://pastebin.com/f68VfFnE

As before when I selected an option in the dropdown box the page went white, and I did saw that the file “ERROR_LOG.php” being created. But I couldnt see any help with its contents being created. They were like:

[24-Jan-2013 20:00:10 UTC] PHP Warning: highlight_file(C:\webserver\www\proj-12-20\ERROR_LOG.PHP): failed to open stream: No such file or directory in C:\webserver\www\proj-12-20\_function_show_setup.php on line 32

[24-Jan-2013 20:00:10 UTC] PHP Warning: highlight_file(): Failed opening ‘C:\webserver\www\proj-12-20\ERROR_LOG.PHP’ for highlighting in C:\webserver\www\proj-12-20\_function_show_setup.php on line 32

So I think I am making mistake in calling the function you gave me. Please let me know what I should do, also please let me know if you need more info.

Thanks

@learner101

I downloaded your files, amended and uploaded the amendments to http://johns-jokes.com/downloads/sp-a/learner101/

The error file is now working and can be called from index-001.php

All javascript has been disabled and the submit button now calls “login_authentication.php”.

I have spent a lot of time removing errors but have also left numerous other for you to become familiar with debugging :slight_smile:

I think JavaScript is a luxury and best if disabled. Get the form to do what you want it to do. Once satisfied then add JavaScript to do the clever stuff.

Also notice the source files are available in learner101.zip