The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Sessions over multiple pages - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Miscellaneous (https://userspice.com/forums/forumdisplay.php?fid=28)
+--- Forum: Off-topic Discussions (https://userspice.com/forums/forumdisplay.php?fid=10)
+--- Thread: Sessions over multiple pages (/showthread.php?tid=1062)



Sessions over multiple pages - justawebbie - 05-21-2018

I am new to sessions but I was trying to carry variables from my custom forms over several pages. My variables are not carrying from page to page. I added the session start function to my header as it was mentioned in an article but I also found info on putting it all in one step above the form:
`<?php
// Start the session
session_start();
?>`
My first page form looks like this:
<pre>
Code:
<form method="post" action="form2.php">
    <input type="text" name="name">
    <input type="text" name="email_address">
    <input type="submit" value="Go To Step 2">
</form>
</pre>

Second form, session is in header as described above:
<pre>
Code:
<?php
//now, let's register our session variables
session_register('name');
session_register('email_address');
//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['email_address'] = $_POST['email_address'];
?>
<form method="post" action="form3.php">
<select name="db_name">
   <option value="1">Database 1</option>
   <option value="2">Database 2</option>
   <option value="3">Database 3</option>
</select>
<input type="radio" name="membership_type" value="Free">
<input type="radio" name="membership_type" value="Normal">
<input type="radio" name="membership_type" value="Deluxe">
<input type="checkbox" name="terms_and_conditions">
<input type="submit" value="Go To Step 3">
</form>
</pre>

My third page, session start is in the header:
<pre>
Code:
<?php
//now, let's register our session variables
session_register('db_name');
session_register('membership_type');
session_register('terms_and_conditions');

$_SESSION['db_name'] = $_POST['db_name'];
$_SESSION['terms_and_conditions'] = $_POST['terms_and_conditions'];
$_SESSION['membership_type'] = $_POST['membership_type'];
?>
<form method="post" action="form_process.php">
<input type="text" name="name_on_card">
<input type="text" name="credit_card_number">
<input type="text" name="credit_card_expiration_date">
<input type="submit" value="Finish">
</form>
</pre>


What am I doing wrong? Do I need to use the session which userspice uses to accomplish this task. If so how would I do that.




Sessions over multiple pages - Brandin - 05-21-2018

When you're calling session_start(), you're resetting the session, or risking it will produce an error message since there is already a session created. Your best option is it just call init.php as recommended and then use session variables like $_SESSION['myVariable']=12345; and you call this by $_SESSION['myVariable'];


Sessions over multiple pages - Brandin - 05-21-2018

Another good way to verify your session data, is during development, adding the following to the bottom of page_footer.php.

Code:
<?=dump($_SESSION)?>



RE: Sessions over multiple pages - justawebbie - 07-09-2018

(05-21-2018, 08:35 PM)Brandin Wrote: Another good way to verify your session data, is during development, adding the following to the bottom of page_footer.php.

Code:
<?=dump($_SESSION)?>

Thank you for the information Brandin. I had health issues but now getting back to the grind. If I have more issues with this I will let you know.

If I do not want my form to make new columns in userspice tables but just have the session carry variables from page to page and let my ajax handle the input of data into my C ++ database would I still call the init.php or just add session variables into session as I move to page to page? I am sorry very new to all this and trying to wrap my head around all this.


RE: Sessions over multiple pages - Brandin - 07-09-2018

init.php will call Session_Start for you and handle communication with the UserSpice Database.