Jump to content

variable Etiquette or... to make new or reuse


flhtc

Recommended Posts

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

Link to comment
Share on other sites

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']);
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.