Jump to content

Sessions and if statements


XpertWorlock

Recommended Posts

Hi, I was wondering whats the proper way of making a session output code?  Example below, an if statement? 

 

<?php
session_start();

$_SESSION['mainData'] = ("

if (strlen  (".$_SESSION['userNameError'].") > 0)
<br><li>
echo ".$_SESSION['userNameError'].";

");



?>

 

 

Instead it outputs :

 

if (strlen () > 0)

# echo ;

 

Link to comment
https://forums.phpfreaks.com/topic/115351-sessions-and-if-statements/
Share on other sites

sorry I should've explained

 

 

 

instead of using a form and having it post back with the error.  The error is determined and than saved as a session and the page is reloaded and displaying the error, and than it unsets the session.

 

There is more to the code than that, it's just a lot easier for people to decipher when it isn't a page and a half.

I think you would do

 

<?php
if (isset($_POST['field_name'])) {
     //do errorchecking code
     // output a sanitized $_POST['field_name']
    //if for some reason a sanitized $_POST['field_name'] can not be created, then print an error message and redisplay form
}
?>

 

sorry but without knowing your purpose, i cant get more specific, but my example doesnt use and doesnt need sessions

 

EDIT: as long as your form posts to $_SERVER['PHP_SELF'] the page reloads when you hit submit and you dont need to store anything in sessions because it reloads with the values previously submitted in your form

$_SESSION['mainData'] = ("


if (strlen  (".$_SESSION['userNameError'].") > 0)
/*<br><li>*/
echo ".$_SESSION['userNameError'].";

");



siteLayout();

 

 

 

The Session(mainData) asks an if statement and than outputs.  siteLayout() outputs a bunch of stuff including Session(mainData).

 

P.S.  Maybe an easier way to ask, how do I get variables to appear/output in sessions.

P.S.S.  How do I write code into Sessions

 

 

EXAMPLE OF SITE DESIGN

 

siteLayout() holds all the design of the page and includes empty sessions into the design.

for each new page I only have to write the code for the body of the page and nothing else.  But now I need to write code into the body.

"for each new page I only have to write the code for the body of the page and nothing else"

 

i did the same thing but made 2 functions, 1 for everything before the body and one for eveerything after

BUT

if you want your session variables to make up the body code, then when you post a form make

 

$_SESSION['blah'] = $_POST['form_data'], where form_data contains all your code in a string

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.