sessions for a newbie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
papaponji
Forum Newbie
Posts: 3
Joined: Wed Jan 10, 2007 9:54 pm

sessions for a newbie

Post by papaponji »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi everyone,

i am a php newbie and just registered a few minutes ago here in devnetwork to do some reasearch on developing my first web page. i started studying php for about less than two weeks and now, i am having a trouble on session variables.

i created a simple web page where you can upload an download files.
uploading and downloading works good. the problem only occured when i tried to put additional codes to authorize if the user had entered the correct login.

btw, i use only one file to do all these and check the availability of session variables.

Code: Select all

<?php
  session_cache_limiter("private");
  session_cache_expire(77);
  session_start();
  $cSelf = $_SERVER['PHP_SELF'];

  if (!session_is_registered('cUsername'))
     { if (!isset($_POST["cUsername"]))  // Checks if the previous link posted a username (and password)
          { echo('<form name = "frmLogin" method = "POST" action = "' . $cSelf . '" 
	        onSubmit = return CheckFields(this)>');
            // do some HTML codes here.
             exit;
          }
     }

  $cGoBack = ' <a href="' . $cSelf . '">Click here </a> to go back';

  if (isset($_POST['cUsername']) && isset($_POST['cPassword']))
     { $cUser = $_POST['cUsername'];
        $cPw = $_POST['cPassword'];

        if ($cUser <> 'myusername' || $cPw <> 'mypassword')
          { echo("Invalid username or password.");
             echo($cGoBack);
             exit;
           }
      }

// Now, register the cUsername
  session_register('cUsername');

// Check if Username is successfully registered:
  echo($_SESSION['cUsername');
// wow! it displays my username. fantastic! now i know it works at this point.

  function ListFiles($cFolderToListFiles, $Header)
    { // some codings to display all the contents of a directory } 

   if (!isset($_FILES['uploadedfile']['name']))
     { // check first if there are files cached (waiting to be uploaded to the server)
        // Calling the function ListFiles() to list all files:
        ListFiles("mydirectory/", "List Of my files in mydirectory:");
        exit;
     }


/* now the line that goes below are my codes to upload the file.
 it now stops here and go back to the top of the line where the server doesn't
 recognize my username anymore. i mean it returns an empty string */
?>

before, without authorization, uploading and downloading works just fine. but after putting extra codes, it seems that the server automatically unregisters my session variable after the page refreshes.

thanks in advance.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by papaponji on Thu Jan 11, 2007 1:44 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure you want this in the phpdeveloper.org forum?
papaponji
Forum Newbie
Posts: 3
Joined: Wed Jan 10, 2007 9:54 pm

Post by papaponji »

my apologies for omitting php tags. i will surely observe that for the next time.

> Are you sure you want this in the phpdeveloper.org forum?

if i came to the right place, yes.[/syntax]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The phpdeveoper.org forum is for posts related to the phpdeveloper.org web site. It is a member of thePHP Developers Network. I think you wanted the PHP Code forum.

Try getting rid of the session_register call. You should be assigning your session variables to the $_SESSION superglobal array.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Yes, session_is_registered() and session_register() are done on old version of php.

for session_is_registered()

Code: Select all

isset($_SESSION['session_var'])
to set the session variable

Code: Select all

$_SESSION['session_var'] = $var
papaponji
Forum Newbie
Posts: 3
Joined: Wed Jan 10, 2007 9:54 pm

Post by papaponji »

oops... i didn't know. i'm sorry guys...

and thank you.

see you in other fourms...

thanks ;)
Post Reply