sessions for a newbie
Posted: Wed Jan 10, 2007 11:22 pm
feyd | Please use
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]
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]