Page 3 of 3 FirstFirst 123
Results 21 to 29 of 29

Thread: If() Elseif()

  1. #21
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    BUT at the moment I am trying to use cookies, please tell me where my problem is.

    edit: nevermind, I suppose I'll use Sessions for now.

    edit2:

    new code:

    page insert:
    Code:
    <?php
    session_start();
    $_SESSION['password']=$password;
    
    if ($password=="password1") {
    print ("logged in. pass: password1
    <br>
    <br>
    <a href=\"phpscripts/logout.php\">Logout</a>");
    }
    
    elseif ($password=="password2") {
    print ("logged in. pass: password2
    <br>
    <br>
    <a href=\"phpscripts/logout.php\">Logout</a>");
    }
    
    elseif ($password=="password3") {
    print ("logged in. pass: password3
    <br>
    <br>
    <a href=\"phpscripts/logout.php\">Logout</a>");
    }
    
    else {
    print ("Login please.
    <br>
    <br>
    Password:
    <br>
    <form method=\"POST\" action=\"phpscripts/login.php\">
    <input class=\"field\" type=\"text\" name=\"password\" size=\"7\">
    <br>
    <br>
    <input onmouseover=\"this.src='images/buttons/loginbutton_mouseon.gif';\" onmouseout=\"this.src='images/buttons/loginbutton_mouseoff.gif';\" type=\"image\" src=\"images/buttons/loginbutton_mouseoff.gif\" alt=\"Login\">
    </form>");
    }
    ?>
    login.php
    Code:
    <?php
    if(($_POST['password'])=="password1") {
    session_start();
    
    $_SESSION['password']='password1';
    print("pass1");
    }
    
    elseif(($_POST['password'])=="password2") {
    setcookie("password", "password2", time()+604800);
    print("pass2");
    }
    
    elseif(($_POST['password'])=="password3") {
    setcookie("password", "password3", time()+604800);
    print("pass3");
    }
    
    else {
    print("no pass");
    }
    ?>
    note: yes i know i only converted pass1 to sesions, it was just to test so i didn't change them all.

    Any clue as to what I've broken????? I thought everything was correct...
    Last edited by TimFA; 12-22-2007 at 07:24 PM.

  2. #22
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I told you that you should sessions, they are more reliable.
    But your origanal problem was that you never unset the cookies, you would be logged in forever, just set the cookie blank if you want to log out.
    --
    Anyways, what's that problem, we can't help if you just say "Its broken"
    What's the error, what does not work?
    Last edited by fileserverdirect; 12-23-2007 at 12:28 AM.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. #23
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    My previous post said the error, and no the problem was not that I wasn't un-setting the cookies, they had been cleared then I created a logout, still no effect, finally I changed the code (I remember where I had left out some quotes that were needed) and it suddenly stopped. The problem is that it will not remember me, with cookies or sessions.

  4. #24
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That script seems ok, but I changed a few things last time I posted. I'll explain why and maybe it will help. This isn't meant to be an attack on your code, but just a way it could be improved some:
    PHP Code:
    $fp fopen('flat-file-data.txt','r'); 
    $line fgets($fp1024);
    //I don't see any need to fopen/fgets, when you can just use file_get_contents().
    //that also avoids any permission errors that would be encountered with fopen.
    list ($passwordlvl1$passwordlvl2$passwordlvl3) = split ('\|'$line); 
    //list is fine, but what if there are more listed in the file?
    //certainly you don't need to use an external file for 3 passwords
    //using split is also a bit weird. explode() doesn't require regex, and "|" would be valid, no need for \|.


    if($_SESSION['password']==$passwordlvl1){//password #1
    }elseif($_SESSION['password']==$passwordlvl2){//password #2
    }elseif($_SESSION['password']==$passwordlvl3){//password #3
    }
    //ok, this would work.
    //however, I would store a "logged in" variable in the session, not the password
    //if ($_POST['pass']=='pass1') { $_SESSION['loginlevel'] = 1; }

    else
    {
    header("location: login.php"); //fine, but technically should be a full URL for some reason

    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #25
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    Thank you drj, but can't someone just tell me whats wrong with that one???? Why can't it remember me??

  6. #26
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    His script was easier to correct because I can easily follow the logic.

    Here are a couple things to improve:

    1. $_SESSION['password']=$password;
    That looks backwards to me. $password was never set, so I think you want to get the pass FROM the session, not the other way around. That is probably the biggest issue.

    2. You are still using cookies, not session variables for the login.
    Place session_start() at the top of the pages. That's it. Then you can save any value to $_SESSION['...'] and it will be available on any other page with session_start() at the top for the duration of your session. So, replace the set_cookie() functions with storing the value to an index of the $_SESSION array.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #27
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    If you read my code, the first one, which I always type in ("password1") IS properly configured for sessions. My problem was the $password thing, I was thinking along the lines of: Get the session data, then turn it into a variable. So I screwed up my order. Thank you alot djr.

    Code:
    if(($_POST['password'])=="password1") {
    session_start();
    
    $_SESSION['password']='password1';
    print("pass1");
    }
    that is the code for login.php, unless I'm mistaken that is correct.

    edit: i correct that one thing, my reverse order and now all is perfect, thank you everyone for your help.

  8. #28
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Seems reasonable, though you might want to do if (isset($_POST['password'])), then check it from there.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #29
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    What difference does it provide? How would I go about sending the user back to the previous page? I tried javascript.go.history(-1); but that doesn't refresh the page, so no luck there. I thought about getting the referring URL, but thats not always correct, or reliable. So whats my best bet? Do I have to send them back to the homepage?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •