Unable to retrieve session variable without commenting a line

I am trying to retrieve/maintain session variable for user across all the three PHP pages as shown below. I am noticing some problems in page3.php in getting the user name from session variable. So in page3.php when I comment the line require('../myDBFolder/dbconn.php'); as I have done in the code below, I can see the user variable value getting printed using he var_dump("Checking for User at the Start: ".$user); However, it doesn’t work and print anything if I uncomment the require. Could anyone tell me what’s wrong with my code?

page1.html

<form method="post" action= "page1.php"  name="lform">
  <span class="style1">User Name :</span>  
    <input type="text" name="user" size="25">
    <input type="submit" value="login">
</form>

dbconn.php

<?php
session_start(); 
    
$user = $_POST["user"]; 
$_SESSION['username']=$user;

$db_server		= "localhost"; 
$db_name		= "PracticeDB"; 
$db_user		= $user;

$connection = mysqli_connect($db_server,$db_user,$db_password) or trigger_error("Could Not Connect to the Database :   ". mysqli_connect_error(), E_USER_ERROR);
$db = mysqli_select_db($connection , $db_name) or trigger_error("Could Not Select the Database : " . $db_name . ':' .mysqli_error($connection));
?>

page2.php

<?php
session_start();
require('../myDBFolder/dbconn.php');

$user = $_POST["user"]; 
$_SESSION['username'] = $user;

$sql="SELECT * FROM $table_name_users WHERE username = \"$user\"";
$result=mysqli_query($connection,$sql) or trigger_error("Couldn't Execute Query in page2.php: ". mysqli_error($sql));
$num = mysqli_num_rows($result);

if ($num != 0) {

	print "<script>";
	print "self.location='page3.php';";
	print "</script>";

} else {
echo "<p>you're not authorized";
}

?>

page3.php

<?php
session_start();

//require('../myDBFolder/dbconn.php');

$user = $_SESSION['username'];
var_dump("Checking for User at the Start: ".$user);

?>

You have session_start() in your include file, so that in page 3 you will call session_start() twice. I don’t know if that will cause a problem or not, but it’s worth checking out.

Hi @WebMachine,

Thanks for your reply. I tried removing session_start() from the dbconn.php file and it didn’t work.

Not sure why you’re connecting to the database in each file. Unless you’re dropping the connection there shouldn’t be the need to keep (re)connecting.

@Gandalf In page3.php, there are some SQL statements which I have’t mentioned above. Do you think the connection isn’t required on that page and connection made from page2.php would be sufficient?

So long as you’ve not closed the db connection, yes it should still be open, so connecting again is at best superfluous.

Try temporarily adding the following to the top of each page and errors or warnings should be displayed.

<?php
error_reporting(-1);
ini_set("display_errors,"true");

Hi @Gandalf,

One reason I noticed why the connection was there in page3.php was because of the following reason:

As I mentioned above, there are some SQL statements in page3.php , so say for example, the SQL statements is like this

$sql = "SELECT * FROM $table_name_data WHERE username = '$user'";

I also have table name defined in the dbconn.php which I didn’t include above. So, in my dbconn.php, I have one more entry like this $table_name_data = "collegestudents";

So, if I remove the require('../myDBFolder/dbconn.php'); from page3.php, then I get errors like

Undefined variables : $table_name_data etc

Could you tell me how to overcome this problem?

If you’re not applying the SELECT to different tables I would be inclined to hard-code the table name and simply have

$sql = "SELECT * FROM collegestudents WHERE username = '$user'";

but there may be reasons I’m not aware of for having it in a variable.

Hmm, with that, the undefined variable related error is no longer showing up but it’s complaining about $connection variable in page3.php because I am trying to do the following in page3.php and $connection variable is defined in dbconn.php:

$sql = "SELECT * FROM collegestudents WHERE username = '$user'";
$result = mysqli_query($connection,$sql) or trigger_error("Could Not Execute the Query ! :   ". mysqli_error($connection));

Ah yes - I can see how that might be a problem! :frowning:

Well, perhaps $connection needs to be put into a session variable then.

Yeah but I was looking at this thread and people are saying that it’s not a good idea to use $connection into a session variable.

Good point. I’m running out of ideas…

Your form’s got for the action page1.php but you’re using page2.php to do the processing, is that just a typo in the form?

No worries. Thanks for the help so far.

Yes, that’s a typo. It should be page2.php. Sorry for the typo.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.