Jump to content

session_start


steveo314

Recommended Posts

Are the first and second scripts on the same website? If so, try simplifying the process. Build a script that does nothing but create a session variable. Then create a second script that only attempts to display the variable. Of course, you'll still need to call session_start(). Do the scripts work if you visit the first and then the second?

 

Are you using sessions successfully elsewhere on the same website?

Link to comment
Share on other sites

Are the first and second scripts on the same website? If so, try simplifying the process. Build a script that does nothing but create a session variable. Then create a second script that only attempts to display the variable. Of course, you'll still need to call session_start(). Do the scripts work if you visit the first and then the second?

 

Are you using sessions successfully elsewhere on the same website?

 

I am trying to work on simplifying the whole thing. The process is spread out over too many scripts.

Do you manage the server where these scripts are posted or does someone else? If someone else, did you check with your host to see if something changed with PHP sessions?

We use a host. They handle the os admin. Whatever files and scripts are on it other than that I manage. They don't do earth shaking updates without notification. This came up when I was still on PHP5.6 and they put me at PHP7 yesterday. I have checked with them several times on what they have changed since this came up. Nothing until yesterday.

Link to comment
Share on other sites

I am trying to work on simplifying the whole thing. The process is spread out over too many scripts.

 

Just to clarify, the simplified scripts would only be for testing. Basically, you can ignore everything else. We just want to know if session variables are actually working. For example, here would be script1.php:

<?php
session_start();
$_SESSION['test'] = 'hello';
?>
<a href="script2.php">Go to next script</a>

Here is script2.php:

<?php
session_start();
print '<pre>' . print_r($_SESSION, true) . '</pre>';
?>
Link to comment
Share on other sites

 

Just to clarify, the simplified scripts would only be for testing. Basically, you can ignore everything else. We just want to know if session variables are actually working. For example, here would be script1.php:

<?php
session_start();
$_SESSION['test'] = 'hello';
?>
<a href="script2.php">Go to next script</a>

Here is script2.php:

<?php
session_start();
print '<pre>' . print_r($_SESSION, true) . '</pre>';
?>

script1.php gives:

Go to next script

script2.php gives:

Array
(
)

Notice: Undefined index: username in /home/website/script1.php on line 14

Notice: Undefined index: test in /home/website/script2.php on line 15

line 14 is:

$username = $_SESSION['username'];

line 15 is:

$test = $_SESSION['test'];

cyberRobot, this is lines 1-13 of script2.php when I ran it with your debug lines:

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(session_status() != PHP_SESSION_ACTIVE) {
    echo "Session not active, script2.php";
    sleep(1);
}
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
//if($_SESSION['username'] == "") {
  //echo "Username is blank";
  //sleep(1);
//}
Link to comment
Share on other sites

Why not show us the first 15 lines of script1

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(session_status() != PHP_SESSION_ACTIVE) {
    echo "Session not active, script1.php";
    sleep(1);
} 
$iconnect = mysqli_connect("localhost","user","pass","db");
if(isset($_POST['submit'])) {
	//$username = htmlentities($_REQUEST['username']);
	//$password = htmlentities($_REQUEST['password']);
	$username = $_POST['username'];
	$password = $_POST['password'];
	$_SESSION['test'] = 'hello';
	$_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
	$password = md5($password);
	$valid = checkinput($username,$password);
	if($valid == true) {
		$valid = checkuser($username,$password,$iconnect);
		if($valid == true) {
			if($username != "") {
				header( 'refresh: 0; url=https://website/script2.php' );
				exit;
Link to comment
Share on other sites


function checkinput($username,$password) {
if($username == "" or $password == "") {
return false;
} else {
return true;
}
}

function checkuser($username,$password,$iconnect) {
$checkquery = "SELECT username, password FROM users WHERE username='$username' AND password='$password'";
if($result = mysqli_query($iconnect,$checkquery)) {
return true;
} else {
return false;
}
}

function failed($reasons) {
echo "<p>There are some problems with your login credentials:</p><p><b>$reasons</b></p>";
}
Link to comment
Share on other sites

From what I can see of your code, you have no $_POST array since I don't see any form code nor any form begin displayed from script1.  

script1.php is the action of the html form which is in reply #19. Do I need to have more in the lines I shared of script1.php in reply #36?

Link to comment
Share on other sites

the error_reporting and display_errors settings need to go before the session_start(), so that any errors with the session start will be reported and displayed.

 

are you on a cheap/free web host that might have disabled the error_reporting() and ini_set() statements so that the error_reporting and display_errors settings aren't doing anything?

Link to comment
Share on other sites

the error_reporting and display_errors settings need to go before the session_start(), so that any errors with the session start will be reported and displayed.

 

are you on a cheap/free web host that might have disabled the error_reporting() and ini_set() statements so that the error_reporting and display_errors settings aren't doing anything?

I moved those two lines up on each script. No additional errors besides whats in reply #34. 

 

I don't know how Westhost fares...

Link to comment
Share on other sites

When I do this on script2.php $_SESSION isn't empty

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
session_start();
echo session_id();
echo '<pre>'.print_r($_SESSION,true).'</pre>';

But when I change it to this it is empty

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
session_start();
echo '<pre>'.print_r($_SESSION,true).'</pre>';

The session ids match between script1.php and script2.php no matter what also.

Link to comment
Share on other sites

  • 2 weeks later...

Do you manage the server where these scripts are posted or does someone else? If someone else, did you check with your host to see if something changed with PHP sessions?

I didn't think thats what it was but....

 

There is a php.ini on a root dir that I cannot get to on my server. My hosting company has access to it. Something was changed on it. session dir was commented out on my php.ini, which overrides theirs. A month and a half of thinking I had a bad piece of code and it was because something was commented out in my php.ini.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.