flhtc Posted March 1, 2008 Share Posted March 1, 2008 Hopefully a simple question. When using session variables, is it better to make new variables or reuse old ones making sure that they are unset after each use? I have several different functions that return a user name or login name and, whether or not the function was successful. Is it better to use a different variable for each function or reuse a single variable, either way I would make sure it is unset after each use. Or does it really make a difference? Thanks, FLHTC Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 Can you post an example of what your talking about? I'm finding it hard to understand what you meen. Quote Link to comment Share on other sites More sharing options...
flhtc Posted March 1, 2008 Author Share Posted March 1, 2008 Ok, the procFTPRegister() function is called. It in turn set a session variable(first code snippet) $_SESSION['ftpreguname'] = $_POST['vnd_login']; which upon return to the program uses that variable (second code snippet) to display the user name different messages depending upon the success or failure of the functioon. There are other function that do other things that if I can reuse the variables would make it easer to program I.E. less to remember. function called: <?php function procFTPRegister(){ global $session, $form; /* Convert usr_login to all lowercase (by option) */ if(ALL_LOWERCASE){ $_POST['vnd_login'] = strtolower($_POST['vnd_login']); } /* FTP Registration attempt */ $retval = $session->ftpregister($_POST['vnd_login'], $_POST['vnd_company'], $_POST['vnd_contact'], $_POST['vnd_phone'], $_POST['vnd_site'], $_POST['vnd_email'], $_POST['subftpregister']); /* Registration Successful */ if($retval == 0){ $_SESSION['ftpreguname'] = $_POST['vnd_login']; $_SESSION['ftpregsuccess'] = true; // header("Location: ".$session->referrer?user=$_POST['vnd_login']); } /* Error found with form */ else if($retval == 1){ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); // header("Location: ".$session->referrer?user=$_POST['vnd_login']); } /* Registration attempt failed */ else if($retval == 2){ $_SESSION['ftpreguname'] = $_POST['vnd_login']; $_SESSION['ftpregsuccess'] = false; // header("Location: ".$session->referrer."?user=$_POST['vnd_login']"); } header("Location: ".$session->referrer."?user=$_POST[vnd_login]"); } ?> return <?php if(isset($_SESSION['ftpregsuccess'])){ /* Registration was successful */ if($_SESSION['ftpregsuccess']){ echo "<h1>Registration Sucessful for: <font color='#ff0000'> $_GET[user]</font></h1><br><br>"; echo "<a href=\"../index.php\">Back to Main</a></p>"; echo "<a href=\"register_ftp_user.php\">Register another FTP User</a>"; } /* Registration failed */ else{ echo "<h1>Registration Failed for: <font color='#ff0000'> $_GET[user]</font> </h1>"; echo "<p>We're sorry, but an error has occurred and your registration for the usr_login <b>" .$_SESSION['reguname']."</b>, " ."could not be completed.<br>Please try again at a later time.</p>"; } unset($_SESSION['ftpregsuccess']); unset($_SESSION['ftpreguname']); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 Your question still eludes me. Quote Link to comment Share on other sites More sharing options...
flhtc Posted March 1, 2008 Author Share Posted March 1, 2008 I was hoping this would be an easy question. Is it better to make new session variables that return similar values for each function, or reuse them? Keeping in mind that they would be unset after each use. I.E. function this() $retval=do somthing if($retval == 0){ $_SESSION['ftpreguname'] = $_POST['vnd_login']; $_SESSION['ftprsuccess'] = true } else{ $_SESSION['ftprsuccess'] = false } function that(){ $retval=do more f($retval == 0){ $_SESSION['ftpreguname'] = $_POST['usr_login']; $_SESSION['ftprsuccess'] = true } else{ $_SESSION['ftprsuccess'] = false } is the same variable Ok, or should I use a different one for each function even though they are being unset after each use? Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 Just as easy to use the same variables over again, unless you need them. Quote Link to comment Share on other sites More sharing options...
flhtc Posted March 1, 2008 Author Share Posted March 1, 2008 Thanks!!! They are only used to return user names etc... They are unset after each use so... I really didn't see a need to make new ones for each function. I just didn't know whether or not it was best to make new ones or reuse the old ones. In my case, it's much easier to reuse them since their use is so limited, and copying scripts and modifying them at least in this context makes my life a whole lot easier. FLHTC P.S. How do I mark this as solved? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.