× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sessions over multiple pages
#1
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.

  Reply
#2
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'];
  Reply
#3
Another good way to verify your session data, is during development, adding the following to the bottom of page_footer.php.

Code:
<?=dump($_SESSION)?>
  Reply
#4
(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.
  Reply
#5
init.php will call Session_Start for you and handle communication with the UserSpice Database.
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)