Cannot send session cache limiter Session_start() warning

The site to test the plugin would be:

http://ifuckinghate.org/newsite/plugintest/
https://i.gyazo.com/2ef05839cdf7d96528e2cd978c45bc7c.png
Test User ID: 13
Test Pin: 246813

Code I am having issues with:

function form_creation(){

		return '<form action="" method="post">
      <label for="name">Patient ID</label>
	  <input  type="text" name="name">
      <label for="pin">Patient PIN</label>
	  <input  type="text" name="pin"><br><br>
      <input  type="submit" name="submit" value="Search">
	  <input type="hidden" name="mmj-submission" value="1" />
    </form>

	'	;

	}

function mmj_code() {

	if ( isset( $_POST['mmj-submission'] ) && '1' == $_POST['mmj-submission'] ) {

$user_id=$_POST['name'];
$pin=$_POST['pin'];

echo "<h2 style='padding-top: 50px;'>Search Results:</h2>";
echo "<p style='text-align: center;'>";

//If they did not enter a search term we give them an error
if ($user_id == "" & $pin == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}

// Otherwise we connect to our Database
mysql_connect  ("localhost", "******", "******") or die(mysql_error());
mysql_select_db("abwebinc_wrdp8") or die(mysql_error());

// We perform a bit of filtering

$pin = strtoupper($pin);
$pin = strip_tags($pin);
$pin = trim($pin);
$user_id = strtoupper($user_id);
$user_id = strip_tags($user_id);
$user_id = trim($user_id);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM wp_mmj WHERE user_id='$user_id' AND user_pin='$pin'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo "<b>Name:</b> " . $result['name'];
echo "<br> ";
echo "<b>ID Number:</b> " . $result['user_id'];
echo "<br> ";
echo "<b>Expiration Date:</b> " . $result['expiration'];
echo "<br>";
echo "<b>This person is FULLY VERIFIED in our system and is a legal Proposition 215 patient.</b>";
echo "<br>";

}

//This counts the number or results
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}

//And we remind them what they searched for
echo "<b>You are viewing the record for User ID:</b> " .$user_id . "<br>";
echo "<b>With the pin of:</b> " .$pin;

echo "</p></center>";

	}
}
add_action( 'init', 'mmj_code' );
add_shortcode('mmj_form', 'form_creation');

I am ATTEMPTING, but failing, at getting the form to submit and run on the same page. As of now, once you submit the form, it will send you to another page and give you the session_start warning.

EDIT:*** first time posting on this site, I think I may have done it wrong...

  • Adam Czajczyk

    Hello Andrew,

    I hope you're well today and thank you for your question!

    I reviewed your code and since it's quite "site specific" I'm not able to really test it but I'm pretty sure that the culprit here is most likely the way you handle communication with the database.

    You are using "mysql_connect()" which should not be used any longer in PHP as it's been deprecated. I suppose it's causing an error here. If it was a "pure PHP" script a "mysqli_connect()" should be used:

    http://php.net/manual/en/function.mysqli-connect.php

    If "mysql_connect()" or any subsequent DB related code is causing any error, that results in breaking "WP flow" and the warning that you get.

    In WordPress however you should never use such functions at all. WordPress does include database handling tools and those should be used. Take a look here please:

    https://codex.wordpress.org/Class_Reference/wpdb

    Refactoring the code to follow WP convenction by changing way of DB connection handling (following article above) should help. If you have any further questions on this, let me know please.

    Kind regards,
    Adam

    • Andrew Giardina

      Thank you for your reply Adam. Much appreciated!

      I was going to be changing everything over today towards the WordPress database handling procedures. Kinda just went for what I new first.

      As for getting any errors back, I am not getting anything specific to MySQL. Everything was actually working perfectly up until I decided to put everything inside a function and make a shortcode for the form instead of hard coding it using my themes page builder <code-block> feature.

  • Andrew Giardina

    I was able to change all of the mysql code into the wordpress standard using $wpdb. Getting no errors and still having a session_start() conflicting problem.

    Is there anything else I can include or do for you guys that might help my situation along? I can include test user credentials for my testplugin wordpress site and include full code if needed.

    I am really stuck and I have searched high and low for an answer. I feel like it is the way I am outputting my data from the @wpdb query that is effecting the rest of the page load.

  • Adam Czajczyk

    Hello Andrew!

    The "db" error seemed very likely in that case and that's why I suggested it. Since your code was so specific to the site (db connection, db prefixes etc) I would need to change it first in order to be able to fully test it and testing changed code could actually return different test results - that wouldn't be reliable. That is why I "estimated" rather than tested, I hope that makes sens :slight_smile:

    It's a good news that you have refactored code to follow WP standards. I have visited your test site and it seems to be working fine. I mean that I don't get any errors there when performing a search, it doesn't give session headers errors and any other errors. Did you manage to deal with it?

    If you still require assistance, could you please share the code again - the current one? While its now using WP functions I should be able to test it on my end.

    Best regards,
    Adam

    • Andrew Giardina

      Good morning Adam,

      After working on the code all day I made some progress certain places.

      function form_creation(){
      
      		return '<form action="http://ifuckinghate.org/newsite/calimedcard/index.php/test-results/" method="POST">
            <label for="mmj_name">Patient ID</label>
      	  <input  type="text" name="mmj_name">
            <label for="mmj_pin">Patient PIN</label>
      	  <input  type="text" name="mmj_pin"><br><br>
            <input  type="submit" name="mmj_submit" value="Rec Search">
      	  <input type="hidden" name="mmj-submission" value="1" />
          </form>
      
      	'	;
      
      	}
      
      function mmj_code() {
      
      	if ( isset( $_POST['mmj-submission'] ) && '1' == $_POST['mmj-submission'] ) {
      
      	$user_id=$_POST['mmj_name'];
      	$pin=$_POST['mmj_pin'];
      
      	//If they did not enter a search term we give them an error
      	if ($user_id == "" & $pin == "")
      	{
      	return '<p>You forgot to enter a search term!!!';
      	}
      	global $wpdb;
      
      	// We perform a bit of filtering
      
      	$pin = strtoupper($pin);
      	$pin = strip_tags($pin);
      	$pin = trim($pin);
      	$user_id = strtoupper($user_id);
      	$user_id = strip_tags($user_id);
      	$user_id = trim($user_id);
      
      	//Now we search for our search term, in the field the user specified
      	//$thepostss = $wpdb->get_row( "SELECT * FROM wp_mmj WHERE user_id='$user_id' AND user_pin='$pin'", $user_id , $pin );
      	$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_mmj WHERE user_id=%s AND user_pin=%d", $user_id , $pin ) );
      
      	//And we display the results
      
      	//This counts the number or results
      
      	if (count($thepost)> 0){
      
      		return '<center><b>Name:</b> ' . $thepost->name . '<br> <b>ID Number:</b> ' . $thepost->user_id . '<br><b>Expiration Date:</b> ' . $thepost->expiration . '<br><b>This person is FULLY VERIFIED in our system and is a legal Proposition 215 patient.</b><br><br><b>You are viewing the record for User ID:</b> ' . $user_id . '<br><b>With the pin of:</b> ' . $pin . '</center>';
      	}
      	else{
      		return '<center>Sorry, but we can not find a client matching that User ID and PIN..<br>Please check the information is correct and try again.<br></center>';
      	}
      
      	//And we remind them what they searched for
      	return '<center><b>You are viewing the record for User ID:</b> ' .$user_id . '<br>';
      	return '<b>With the pin of:</b> ' .$pin;
      
      	return '</p></center>';
      
      	}
      }
      
      add_shortcode('mmj_code_shortcode', 'mmj_code');
      add_shortcode('mmj_form', 'form_creation');

      In the above code, I went ahead and took your advice and switched over to $wpdb for the query. MY issue with the session_start() seemed to be specific to ENFOLD theme. My workaround for this was to create two shortcodes. One for the form and one for the query code.

      My form forwards people to the separate page with the second shortcode. It seems I am unable to get everything to run properly on the same page and location.

  • Adam Czajczyk

    Hello Andrew!

    I checked that on my end. I think that the "two shortcodes" solution - even though it was created as a "workaround" - is actually a better way to handle that.

    You should be able however to use both shortcodes on the same page. Furthermore, it's even possible to roll entire code into a single shortcode and keep user on the very same page.

    I'm attaching example code (with additional slight adjustments to how the content is displayed) below:

    function form_creation(){
    
    	if ( isset( $_POST['mmj-submission'] ) && '1' == $_POST['mmj-submission'] ) {
    
    		$user_id=$_POST['mmj_name'];
    		$pin=$_POST['mmj_pin'];
    
    		//If they did not enter a search term we give them an error
    		if ($user_id == "" & $pin == "") {
    			return '<p>You forgot to enter a search term!!!';
    		}
    
    		global $wpdb;
    
    		// We perform a bit of filtering
    
    		$pin = strtoupper($pin);
    		$pin = strip_tags($pin);
    		$pin = trim($pin);
    		$user_id = strtoupper($user_id);
    		$user_id = strip_tags($user_id);
    		$user_id = trim($user_id);
    
    		//Now we search for our search term, in the field the user specified
    		//$thepostss = $wpdb->get_row( "SELECT * FROM wp_mmj WHERE user_id='$user_id' AND user_pin='$pin'", $user_id , $pin );
    		$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_mmj WHERE user_id=%s AND user_pin=%d", $user_id , $pin ) );
    
    		//And we display the results
    
    		//This counts the number or results
    
    		if (count($thepost)> 0){
    
    			$html = '<center><b>Name:</b> ' . $thepost->name . '<br> <b>ID Number:</b> ' . $thepost->user_id . '<br><b>Expiration Date:</b> ' . $thepost->expiration . '<br><b>This person is FULLY VERIFIED in our system and is a legal Proposition 215 patient.</b><br><br><b>You are viewing the record for User ID:</b> ' . $user_id . '<br><b>With the pin of:</b> ' . $pin . '</center>';
    		}
    		else {
    			$html = '<center>Sorry, but we can not find a client matching that User ID and PIN..<br>Please check the information is correct and try again.<br></center>';
    		}
    
    		//And we remind them what they searched for
    		$html .= '<center><b>You are viewing the record for User ID:</b> ' .$user_id . '<br>';
    		$html .= '<b>With the pin of:</b> ' .$pin;
    
    		$html .= '</p></center>';
    
    		return $html;
    
    	}
    	else {
    		$html = '
    			<form method="POST">
    				<label for="mmj_name">Patient ID</label>
    				<input  type="text" name="mmj_name">
    				<label for="mmj_pin">Patient PIN</label>
    				<input  type="text" name="mmj_pin"><br><br>
    				<input  type="submit" name="mmj_submit" value="Rec Search">
    				<input type="hidden" name="mmj-submission" value="1" />
    			</form>
    		';
    		return $html;
    	}
    }
    
    add_shortcode('mmj_form', 'form_creation');

    Give it a try please (you will only need the single [mmj_form] shortcode). It seems to be working fine on my end. I'm not able to test it with Enfold theme unfortunately but I can see absolutely no reason why it could break it (while not breaking other themes) so let's see how this version works for you.

    Best regards,
    Adam

    • Andrew Giardina

      Worked perfectly! Thank you for your help. I was able to make a few more changes and everything seems to be looking good.

      Additional Information for anyone else running into my original problem:
      My original problem was that my plugin form was redirecting to another page and just echoing back results and plugin functions. This is where the issue was coming from. Somehow my echo results would mess with Enfolds session_start() and end up printing the results at top of the page and messing up the rest of the page load.

      To get around it, I first changed all my echo > return, then I actually created the codes own shortcode to add to a separate page for my form to redirect to. (Thanks to Adams help, he has consolidated both functions into a single shortcode allowing the plugin to execute on the same page and same place)

      Here is my final code for those who need it:

      function form_creation(){
      
      	if ( isset( $_POST['mmj-submission'] ) && '1' == $_POST['mmj-submission'] ) {
      
      		$user_id=$_POST['mmj_name'];
      		$pin=$_POST['mmj_pin'];
      
      		//If they did not enter a search term we give them an error
      		if ($user_id == "" & $pin == "") {
      			return '<center><p>You forgot to enter a search term!!!</center>';
      		}
      
      		global $wpdb;
      
      		// We perform a bit of filtering
      
      		$pin = strtoupper($pin);
      		$pin = strip_tags($pin);
      		$pin = trim($pin);
      		$user_id = strtoupper($user_id);
      		$user_id = strip_tags($user_id);
      		$user_id = trim($user_id);
      
      		//Now we search for our search term, in the field the user specified
      		//$thepostss = $wpdb->get_row( "SELECT * FROM wp_mmj WHERE user_id='$user_id' AND user_pin='$pin'", $user_id , $pin );
      		$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_mmj WHERE user_id=%s AND user_pin=%d", $user_id , $pin ) );
      
      		//And we display the results
      
      		//This counts the number or results
      
      		if (count($thepost)> 0){
      
      			$html = '<div id="mmj_form_area" margin="30px"><script>
      document.getElementById("mmj_form_area").style.backgroundColor = "#5bba51";
      document.getElementById("mmj_form_area").style.padding = "30px 30px 30px 30px";
      </script>
      			<center><b>Name:</b> ' . $thepost->name . '<br> <b>ID Number:</b> ' . $thepost->user_id . '<br><b>Expiration Date:</b> ' . $thepost->expiration . '<br><b>This person is FULLY VERIFIED in our system and is a legal Proposition 215 patient.</b><br><br><b>You are viewing the record for User ID:</b> ' . $user_id . '<br><b>With the pin of:</b> ' . $pin . '<br><br>
      			<h2>Search Again</h2>
      			<form method="POST">
      				<label for="mmj_name">Patient ID</label>
      				<input  type="text" name="mmj_name">
      				<label for="mmj_pin">Patient PIN</label>
      				<input  type="text" name="mmj_pin"><br>
      				<input  type="submit" name="mmj_submit" value="Rec Search">
      				<input type="hidden" name="mmj-submission" value="1" />
      			</form></center></div>';
      		}
      		else {
      			$html = '<div id="mmj_form_area" margin="30px"><script>
      document.getElementById("mmj_form_area").style.backgroundColor = "#e93333";
      document.getElementById("mmj_form_area").style.padding = "30px 30px 30px 30px";
      </script>
      			<center>Sorry, but we can not find a client matching that User ID and PIN..<br>Please check the information is correct and try again.<br><br><b>You are viewing the record for User ID:</b> ' .$user_id . '<br><b>With the pin of:</b> ' .$pin . '<br><br>
      			<h2>Search Again</h2>
      			<form method="POST">
      				<label for="mmj_name">Patient ID</label>
      				<input  type="text" name="mmj_name">
      				<label for="mmj_pin">Patient PIN</label>
      				<input  type="text" name="mmj_pin"><br>
      				<input  type="submit" name="mmj_submit" value="Rec Search">
      				<input type="hidden" name="mmj-submission" value="1" />
      			</form></center></div>';
      
      		return $html;
      		}
      
      		return $html;
      
      	}
      	else {
      		$html = '<div id="mmj_form_area" margin="30px"><script>
      document.getElementById("mmj_form_area").style.backgroundColor = "#5bba51";
      document.getElementById("mmj_form_area").style.padding = "30px 30px 30px 30px";
      </script>
      			<center><form method="POST">
      				<label for="mmj_name">Patient ID</label>
      				<input  type="text" name="mmj_name">
      				<label for="mmj_pin">Patient PIN</label>
      				<input  type="text" name="mmj_pin"><br>
      				<input  type="submit" name="mmj_submit" value="Rec Search">
      				<input type="hidden" name="mmj-submission" value="1" />
      			</form></center></div>
      		';
      		return $html;
      	}
      }
      
      add_shortcode('mmj_form', 'form_creation');

      (I will be condensing the additional forms attached to the return $htmls. But I left it for now.)

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.