remember and display name of a session user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    remember and display name of a session user

    I have as my script the following..

    Code:
    <?php session_start();?>
    <html>
    
    <body>I am<?php function setanimal(){echo (isset( $_SESSION['s']));
    								if (isset( $_SESSION['animal'])){
    									return  $_SESSION['animal'];echo  $_SESSION['animal'];}
    								else{
    									return "guets";
    									}
    								echo setanimal();}
    								?>
    <form name="sa" action="hi.php" method="get">
    	<input type="text" name="name">
    	<input type="submit" value="submit">
    </form> 
    <?php if (isset($_GET['name'])){echo "I am working";
    		$_SESSION['animal']=$_GET['name'];}?>
    </body>
    </html>

    I want to diaplay whetever I enter through the inbox...I have stored the contents in a session var......when I press th submit btn the function setanimal() always passes through the else part and display me the "guets".Doe s that mean,the session var has nt been set?How can I diaplay the input contents now?

    Any help will be kindly appreciated.
    thanks in advance
    Last edited by Dormilich; Jan 8 '10, 10:40 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    when I press th submit btn the function setanimal() always passes through the else part and display me the "guets".
    I don’t know which code you use, but that’s the only thing that doesn’t happen. it writes the session var correctly (as I expected). but you don’t call setanimal() anywhere.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Yea, if your code is printing "guets", it is not the code you posted here.
      The "setanimal" function never gets called in this code, except for inside the function itself... Which, coincidentally, proves that it never get called, seeing as it would cause the script to freeze.

      I've added a few comments to your code. Maybe that will help you.
      [code=php]<?php session_start() ; ?>
      <html>
      <body>I am
      <?php
      // This is a function. It doesn't actually get
      // executed unless it is called from OUTSIDE the
      // function itself. Functions should be DEFINED
      // at the top of the script and CALLED where
      // they are to be executed. See the manual
      // for a proper explaination.
      function setanimal()
      {
      // isset returns a boolean, so this doesn't
      // really print anything of use. Try
      // var_dump() instead, for debugging purposes.
      echo (isset( $_SESSION['s']));

      if (isset( $_SESSION['animal'])) {
      return $_SESSION['animal'];

      // There is no point adding anything after
      // "return". At that point the function
      // execution has been stopped.
      echo $_SESSION['animal'];
      }
      else {
      // "guest" ;-)
      return "guets";
      }

      // BEWARE!
      // This will cause the function to enter
      // and indefinate loop, effectively freezing
      // the script whenever this function is called.
      // This line should be removed, or moved out of
      // the body of the function.
      echo setanimal();
      }
      ?>

      <form name="sa" action="<?php $_SERVER['PHP_SELF']; ?>" method="get">
      <input type="text" name="name">
      <input type="submit" value="submit">
      </form>

      <?php
      // You might also want to check if the variable
      // is empty() before using it. As it is, this code
      // will use the name even if it contains no text.
      if (isset($_GET['name'])) {
      echo "I am working";
      $_SESSION['animal'] = $_GET['name'];
      }
      ?>
      </body>
      </html>[/code]

      P.S.
      Note how I formatted my code. How I added line-breaks and indents so that you don't have to struggle to figure out to which block each statement belongs.
      Makes it easier to read, no? ;-)

      Comment

      Working...