Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Session problems

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Session problems

Postby rocky48 » Mon May 27, 2013 1:39 pm

I am trying to use session_start to enable the saving of a variable that I need to use in another script.
I am falling at first base, as when I run the script it keeps say that a session is already running.

I have tried using session_destroy() and even register_shutdown_function('session_write_close'), but still get the message:
"Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /homepages/43/d344817611/htdocs/test/VInput.php:10) in /homepages/43/d344817611/htdocs/test/VInput.php on line 21".

Does this mean that the seesion has not started? or does it need some more parameters set?

some help would be appreciated.
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby johnj » Mon May 27, 2013 8:02 pm

session_start() should be the very first line of your .php file. Make sure that you do not have any white space or file inclusion before that.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Session problems

Postby rocky48 » Fri May 31, 2013 2:00 pm

Thanks but I knew that!
I am really puzzled what is going on.

To test the function of session I have used:
$ses_id=session_id;
echo $ses_id;
and on the preceeding file it prints a long hex string:
b7ed15281a021bc0b3182920265bd4c6.$_POST["EID"].
It keeps displaying the .$_POST["EID"]. even after I changed the script to name=VID
Here is part of the first script to run:
Code: Select all
$display_block = "
   <table width=\"70%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
   <tr>
   <th>ID</th>
   <th>VERSE</th>
   <th>MOOD/SUB TYPE</th>
   </tr>";

   while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
      $Verse_id = $Verse_info['versesID'];
      $Verse_text = nl2br(stripslashes($Verse_info['Verse']));
      $Mood_info = $Verse_info['Event_Sub_Type'];
      
      //add to display
       $display_block .= "
      <tr>
      <td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
      <td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
      <td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
      <td width=\"35%\" valign=\"top\">
      <Form action=\"VInput.php\" .method=.\"POST\">
      <input type=\"Radio\" name=\"VID\" value=\".$Verse_id.\">
      </td></tr>";   
   }
$display_block .= "   
<input \"type=\"submit\" value=\"Choose Verse\">";   
   //free results
   mysqli_free_result($get_Event_res);
   mysqli_free_result($verify_Event_res);   
//close connection to MySQL
   mysqli_close($mysqli);
   
//close up the table
$display_block .= "</table>";   
}
$_SESSION['test']=2222;
?>

As you can see at the end of the script I have also experimented by creating a $_SESSION called test.
This does not print out either.
The second script is an input form, where I am trying to eliminate the need to input the verse id manually, which is the point of this exercise.
Here is the part code that I am trying to see if the variable is being stored using SESSION:
Code: Select all
<?php
$ses_id=session_id();
echo $ses_id;
echo $_SESSION['test'];
var_dump($POST);

?>

Also I have tried a var_dump($POST), that to does not display anything.
I am getting very frustrated with this :help:
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby johnj » Sat Jun 01, 2013 5:11 am

where is your session_start()? why are you not using session_start()? Please read about sessions from the php manual.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Session problems

Postby developerahusain » Sat Jun 01, 2013 6:24 am

session_start ();

<? $_SESSION ['input']="asdf";

echo $_SESSION ['input']; ?>


You put this in a new page and see if this works out and from there you can build on your page. Any problem do revert because i have witnessed lot of stuff like this.
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Session problems

Postby rocky48 » Thu Jun 06, 2013 2:18 am

Right!
The code above works fine apart from the fact that the session_start() should be inside the php tag.
But this did not prove that the string was being kept from file to file, so I changed the session id to 'test' and it printed out the string that I had entered in the previous file.
However I wanted to store the POSTed value VID from the query generated form.
When I run this I got a blank screen i.e. it did not echo the POSTed value, but I noticed that in the address bar it had the following: http://1066cards4u.co.uk/test/VInput1.php?VID=.16.

I assume this meant that the value was being passed by the POST? But why did the SESSION not print the value? Have I got the syntax wrong?

Here is the line that generated the session: $_SESSION['test']="$_POST[VID]";
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby developerahusain » Thu Jun 06, 2013 3:00 am

Rocky you should change this

$_SESSION['test']="$_POST[VID]";

into this

$_SESSION ['test']=$_POST ['VID'];


any problem do revert.

Developerahusain
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Session problems

Postby rocky48 » Thu Jun 06, 2013 6:20 am

Hi
I have tried that,but it does not echo anything on the screen.
This it the code in the first file:
Code: Select all
$_SESSION['test']=$_POST ['VID'];


This is how I am trying to retrieve the session:
Code: Select all
<?
session_start ();
$ver=$_POST['VID'];
echo $_SESSION ['test'];
$ses_id=session_id();
echo $ses_id;
echo $ver;
?>

As you can see I am trying several methods to retrieve the value but it does not output anything, apart from the session id.
Any other ideas?
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby developerahusain » Thu Jun 06, 2013 7:34 am

You must be using a form for getting the VID post value. Can you show me your form. I will be able to solve your problem.

Developer ahusain
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Session problems

Postby rocky48 » Sun Jun 09, 2013 1:46 am

Hi
Developer ahusain

The form is inside a SQL query. I wanted to filter the data that met certain criteria.
Here is the compete script:
Code: Select all
<?php
if(!isset ($_COOKIE[Verse])){
   setcookie("Verse", $_POST['VID'],0);
}
include("cverse_connect.php");
doDB();

//check for required info from the query string


//verify the Event exists
$verify_Event_sql = "SELECT ID, Event_Type FROM Events WHERE ID = '".$_POST["Event_Type"]."'";
$verify_Event_res =  mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli));



//echo  $_POST["Event_Type"];
//echo  $_POST["Mood"];

if (mysqli_num_rows($verify_Event_res) < 1) {
   //this Event does not exist
   $display_block = "<p><em>You have selected an invalid Event.<br/>
   Please try again.</em></p>";

} else {
//get the Event ID
   while ($Event_info = mysqli_fetch_array($verify_Event_res)) {
      $Event_ID = stripslashes($Event_info['ID']);
      $Event_Name = ($Event_info['Event_Type']);
   }

$verify_Mood_sql = "SELECT ID, Event_Sub_Type FROM Event_Sub WHERE ID = '".$_POST["Mood"]."'";
$verify_Mood_res =  mysqli_query($mysqli, $verify_Mood_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($verify_Mood_res) < 1) {
   //this Event does not exist
   $display_block = "<p><em>You have selected an invalid Mood.<br/>
   Please try again.</em></p>";
}

   while($Mood_info = mysqli_fetch_array($verify_Mood_res)) {
      $Mood_ID = ($Mood_info['ID']);
      $Mood_Name = ($Mood_info['Event_Sub_Type']);
   }
   //gather the Events
   $get_Event_sql  = "SELECT Verses.ID AS versesID, Verses.Verse, Verses.Sub_Type, Verses.Event, Events.ID AS eventsID, Events.Event_Type, Event_Sub.ID AS event_SubID, Event_Sub.Event_Sub_Type
   FROM Verses
   LEFT JOIN Events
   ON Verses.Event = Events.ID
   LEFT JOIN Event_Sub
   ON Verses.Sub_Type = Event_Sub.ID
   WHERE Verses.Event = '".$_POST["Event_Type"]."' And Verses.Sub_Type = '".$_POST["Mood"]."'
   ORDER BY Verses.ID ASC";


   $get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));

   //create the display string
   $display_block = "
   <table width=\"70%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
   <tr>
   <th>ID</th>
   <th>VERSE</th>
   <th>MOOD/SUB TYPE</th>
   </tr>";

   while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
      $Verse_id = $Verse_info['versesID'];
      $Verse_text = nl2br(stripslashes($Verse_info['Verse']));
      $Mood_info = $Verse_info['Event_Sub_Type'];
      
      //add to display
       $display_block .= "
      <tr>
      <td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
      <td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
      <td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
      <td width=\"35%\" valign=\"top\">
      <Form action=\"VInput1.php\" .method=.\"POST\">
      <input type=\"Radio\" name=\"VID\" value=\".$Verse_id.\">
      </td></tr>";   
   }
$display_block .= "   
<input type=\"submit\" value=\"Choose Verse\">";   
   //free results
   mysqli_free_result($get_Event_res);
   mysqli_free_result($verify_Event_res);   
//close connection to MySQL
   mysqli_close($mysqli);
   
//close up the table
$display_block .= "</table>";   
}
$_SESSION['test']=$_POST ['VID'];
?>
<html>
<head>
<title> List of Verses</title>
<meta charset="utf-8">
    <meta name="Description" content="Ecologic Theme">
    <meta name="author" content="CoffeeCup Software, Inc.">
    <meta name="Copyright" content="Copyright (c) 2011 CoffeeCup, all rights reserved.">
    <title>1066 Cards 4U - Home</title>
    <link rel="stylesheet" href="stylesheets/default.css" />
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
 <header>
        <h1><a href="index.html"> ..............</a></h1></br></br></br></br>
      <div><img src="images/1066Green.jpg" width="600" height="80" alt="" title="" border="0" /></div>
       
    </header>
<section id="mainContent" class="clear">
        <nav>
            <h3>Main Menu</h3>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="Links.html">Links</a></li>
                <li><a href="Techniques.html">Techniques</a></li>
            <li><a href="Alt_inputform.html">Verse's</a></li>            
                <li><a href="contact.html">Contact Us</a></li>
            <li><a href="News.html">News</a></li>
            <li><a href="Gallery.html">Gallery</a></li>
            </ul>
        </nav>
<section id="mainRight2">      
<h1>Verses</h1>
<FORM METHOD="LINK" ACTION="VInput.php">
<INPUT TYPE="submit" VALUE="Go To Print Utility">
</FORM>
<?php echo $display_block; ?>
</section>
<footer>
        <p>&copy; 2011 Ecologic, Inc. All rights reserved. &nbsp;&nbsp; <a href="http://coffeecup.com">HTML Editor Theme</a> by CoffeeCup Software.</br> &copy; 2012 Content:1066 Cards 4U. All rights reserved. &nbsp;&nbsp;</p>
      <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="Links.html">Links</a></li>
            <li><a href="Techniques.html">Techniques</a></li>
         <li><a href="Alt_inputform.html">Verse's</a></li>            
            <li><a href="contact.html">Contact Us</a></li>
         <li><a href="News.html">News</a></li>
         <li><a href="Gallery.html">Gallery</a></li>
        </ul>
    </footer>
</body>
</html>


I have also just tried to use cookies. That did not echo the cookie value either?
The code shown has been changed for cookies, I removed the session_start() and the code for generating the session output previously listed.

Any other suggestions?
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby developerahusain » Sun Jun 09, 2013 2:03 am

Here is the code rocky.This will definitely print the test session. test this code first without any editing.Then you can modify it as per your requirements.

if ($_POST ['test_submit'])
{
$_SESSION ['test']=$_POST['VID'];
echo "<script language='javascript'>window.location='?'</script>";
}

$display_block .= "
<tr>
<td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
<td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
<td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
<td width=\"35%\" valign=\"top\">
<form .method=.\"POST\">
<input type=\"Radio\" name=\"VID\" value=lajsdf>
</td></tr>";
$display_block.="<input type=submit value=Choose Verse name=test_submit>";

<? echo $display_block; ?>

Developerahusain. Do revert if there is any problem still
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Session problems

Postby rocky48 » Sun Jun 09, 2013 8:49 am

Hi developerahusain

I assume that you meant for me to splice this code into my existing script which queries the database?
I did this and it did the query and presented the form, but when I made a choice, it somehow wound back to the statement that tests whether a choice had been made in the previous script for an event or sub-event.
It jumps to the first if:
Code: Select all
if (mysqli_num_rows($verify_Event_res) < 1) {
   //this Event does not exist
   $display_block = "<p><em>You have selected an invalid Event.<br/>
   Please try again.</em></p>";

} else {
//get the Event ID
   while ($Event_info = mysqli_fetch_array($verify_Event_res)) {
      $Event_ID = stripslashes($Event_info['ID']);
      $Event_Name = ($Event_info['Event_Type']);
   }

I tried changing the value for the radio button to a numeric, as normally VID would be numeric, but the same result happened.
Obviously if I wanted it to use this value on the next script I would have to add action=\"Vinput1.php\".
Any other suggestions?
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby developerahusain » Sun Jun 09, 2013 9:58 pm

I understand your requirements rocky. Because i'm also a developer. I have kept the form action alive just as you said and i modified the script for it. I have tested this. It printed out the session.After checking you can build, modify the code for your use.


<? if ($_POST ['b_test']) {
$_SESSION['test']=$_POST['VID'];

}
echo $_SESSION['test'];
$display_block.="<input type=radio value=2 name=VID>
<input type=submit value=Submit name=b_test>";
?>

<form name="ahamed" method="post" action="test19.php">
<? echo $display_block; ?>
</form>
developerahusain
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Session problems

Postby johnmacd » Thu Jun 13, 2013 10:23 pm

session_start() should be the very first line of your php code .
johnmacd
New php-forum User
New php-forum User
 
Posts: 19
Joined: Wed Jun 12, 2013 9:30 pm

Re: Session problems

Postby rocky48 » Mon Jun 17, 2013 6:16 am

To Johnmacd
Please read ALL of the posts! This has been done in all cases.
Husain
I am having graet difficulty understanding where the code you have written fits into my existing code.
I assume the first 3 lines fit here:
Code: Select all
//create the display string
   $display_block .= "
<table width=\"70%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
   <tr>
   <th>ID</th>
   <th>VERSE</th>
   <th>MOOD/SUB TYPE</th>
   </tr>";   

   while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
      $Verse_id = $Verse_info['versesID'];
      $Verse_text = nl2br(stripslashes($Verse_info['Verse']));
      $Mood_info = $Verse_info['Event_Sub_Type'];
      
      //add to display
   if ($_POST ['b_test']){
   $_SESSION ['test']=$_POST['VID'];
}

$display_block .= "
<tr>
<td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
<td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
<td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
<td width=\"35%\" valign=\"top\"> etc

Why have you echoed the session next?
then I assume that you mean for me to put the line <input type=radio value=2 name=VID> here:
Code: Select all
$display_block .= "
<tr>
<td width=\"1%\" valign=\"top\">".$Verse_id."<br/></td>
<td width=\"55%\" valign=\"top\">".$Verse_text."<br/></td>
<td width=\"35%\" valign=\"top\">" .$Mood_info."<br/></td>
<td width=\"35%\" valign=\"top\">
<form .method=.\"POST\">
<input type=radio value=2 name=VID>
</td></tr>";
}
$display_block.="<input type=submit value=Submit name=b_test>";

What I really don't understand is the last 3 lines:
Code: Select all
<form name="ahamed" method="post" action="test19.php">
<? echo $display_block; ?>
</form>

Why are you starting a form after you have declared the input type and submitted the POST?
The input of the radio button has to be inside the while loop, otherwise you do not get a radio button on each line of the table.
Please advise!
rocky48
New php-forum User
New php-forum User
 
Posts: 16
Joined: Wed Aug 01, 2012 12:57 pm

Re: Session problems

Postby johnmacd » Sun Jun 23, 2013 9:13 pm

Write code for session from first line of your editor.Do not leave blank lines.
johnmacd
New php-forum User
New php-forum User
 
Posts: 19
Joined: Wed Jun 12, 2013 9:30 pm

Re: Session problems

Postby jaysismyson » Fri Jul 05, 2013 1:54 am

I tried changing the value for the Aion Kinah radio button to a numeric.
jaysismyson
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Jul 05, 2013 1:49 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.