Question : PHP Login Script

I am trying to learn PHP a bit more as I did not do so well in it in college and will be taking another course in the University starting in September. While in school I created a small site with a login script. I completed the registration page which works wonderful. I completed the login script which also works wonderful. Here is my issue. I would like all the pages to be password protected. I put this script at the top of each page:

<?PHP

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: famlogin.php");

}else{
$fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE username='".$_SESSION['username']."'"));
}
?>

It does take me to my login page. But what it does not do is keep me logged in. Every time I click on one of the pages it takes me to the login page. The end result is that I cant even see the pages now because it wont give it a chance to stay logged in. I thought maybe if I switched around the  "if" and "else" statement for it to check the user data first and then go to the login script it would work like this:

<?PHP

session_start();
$fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE username='".$_SESSION['username']."'"));

}else{
      
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: famlogin.php");

}
?>

But it only gives me a blank white screen when I go to that page. What am I doing wrong?

Answer : PHP Login Script

<the on click event doesn't want to do anything?>
What "OnClick" event?

You specified that this should happen...:
 "When the user clicks the close button" ("The Close button", ...meaning *the* windows standard close button, the little "X" in the upper right side of a window)
... not...
 "When the user clicks *my* close button.

So our codes are on the Close event and the Unload event receptively.

If you want the code on "Your" close button, then use code like this:

Private Sub btmClose_Click()
Dim bytAnswer As String

    If Instock.Value <> 0 Then
        bytAnswer = MsgBox("Exit now?", vbYesNo, "Exit?")
            If bytAnswer = vbYes Then
                'Do nothing
            ElseIf bytAnswer = vbNo Then
                Me.Instock.SetFocus
                Exit Sub
            End If
    Else
        'Do Nothing
    End If
   
    DoCmd.Close
End Sub

Remember here that you have not told us what other code (if any) is present on your close button already, so this code may need to be tweaked.

Finally, remember that when you code your own close button you have to be sure that you are covering all the contingencies for the various states that the data may be left in.

I am sure 2toria can post similar working code.

;-)

JeffCoachman
Random Solutions  
 
programming4us programming4us