Login system

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frizzle

    #1

    Login system

    Hi Group,

    I have a site with a MySQL backend. It has a member-system.
    Members login with a small login-form that appears on every page
    (via include())
    If members are logged in, the form disappears and a few extra links
    appear instead of the form.

    - If members log in, i want to redirect them, if succesful, back to
    the page they logged in from.
    Should i use an extra hidden form-field with the
    $_SERVER['request_uri'] or sould i use the $_SERVER['http_referer']?

    - In both cases, how can i check that the referer is from MY domain?
    if users login from http://domain.com/page.php i want to send them
    back to that page, and not to http://www.domain.com/page.php and
    vice versa.
    How do i make sure they come from 1 of my own pages, and it's
    accepted WITH and WITHOUT the 'www' prefix?

    Thanks for any help!

    Greetings Frizzle.

  • Justin Koivisto

    #2
    Re: Login system

    frizzle wrote:[color=blue]
    > I have a site with a MySQL backend. It has a member-system.
    > Members login with a small login-form that appears on every page
    > (via include())
    > If members are logged in, the form disappears and a few extra links
    > appear instead of the form.
    >
    > - If members log in, i want to redirect them, if succesful, back to
    > the page they logged in from.
    > Should i use an extra hidden form-field with the
    > $_SERVER['request_uri'] or sould i use the $_SERVER['http_referer']?
    >
    > - In both cases, how can i check that the referer is from MY domain?
    > if users login from http://domain.com/page.php i want to send them
    > back to that page, and not to http://www.domain.com/page.php and
    > vice versa.
    > How do i make sure they come from 1 of my own pages, and it's
    > accepted WITH and WITHOUT the 'www' prefix?[/color]

    In my project (sf.net/projects/phpsecurityadm) I've been using the
    REQUEST_URI. If you use the referer, that's the page they came from, so
    if they have the login bookmarked, then it will be blank. If they have a
    proxy or firewall, that may be blank as well... If you use PHP_SELF,
    then on a site that uses mod_rewrite or the like would have problems
    since it isn't finding what it expects.

    Basically, I have this set up in my login form generation:
    if(isset($_SERV ER['REQUEST_URI'])){
    echo ' <form method="post" action="',
    $_SERVER['REQUEST_URI'],'">',"\n",
    ' <fieldset>',"\n ",
    ' <input type="hidden" name="PSA_REQ_U RI" value="',
    $_SERVER['REQUEST_URI'],'" />',"\n";
    }else{
    echo ' <form method="post" action="',
    $_SERVER['PHP_SELF'],'">',"\n",
    ' <fieldset>',"\n ",
    ' <input type="hidden" name="PSA_REQ_U RI" value="',
    $_SERVER['PHP_SELF'],'" />',"\n";
    }

    This seems to have been working out well for me and other users.

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • frizzle

      #3
      Re: Login system

      Thanks for the reply!

      Reading this, it seems to me, $_SERVER['REQUEST_URI'] isn't always set.
      Isn't this a full server-side property?
      I'm using mod_rewrite, so the second part of your code wouldn't be very
      nice ... :(

      Thanks!

      Frizzle.

      Comment

      • Gordon Burditt

        #4
        Re: Login system

        >I have a site with a MySQL backend. It has a member-system.[color=blue]
        >Members login with a small login-form that appears on every page
        >(via include())
        >If members are logged in, the form disappears and a few extra links
        >appear instead of the form.
        >
        > - If members log in, i want to redirect them, if succesful, back to
        > the page they logged in from.
        > Should i use an extra hidden form-field with the
        > $_SERVER['request_uri'] or sould i use the $_SERVER['http_referer']?[/color]

        http_referer is sent from the browser, so it can't be trusted.
        Also, many people turn it off or send nonsense for it. Some people
        may not be able or willing to easily turn it back on for your site.
        Use your hidden field. You have much more control over it.
        [color=blue]
        > - In both cases, how can i check that the referer is from MY domain?[/color]

        Well, if it's just a random link, it probably won't have your hidden
        form-field with the place to go back to listed. isset($_PUT['go_back_to'])
        might be useful to test this.
        [color=blue]
        > if users login from http://domain.com/page.php i want to send them
        > back to that page, and not to http://www.domain.com/page.php and
        > vice versa.
        > How do i make sure they come from 1 of my own pages, and it's
        > accepted WITH and WITHOUT the 'www' prefix?[/color]

        Parse the URL. If it's www.domain.com, change it to domain.com.
        If it's not on a list of domains that are "yours", or not http or
        https, or the field is missing entirely, send them to your home
        page or someplace default. You could have a complete list of all
        acceptable URLs where you have these login forms, but that's probably
        too much work and not worth it. Just checking the domain is probably
        enough.

        Gordon L. Burditt

        Comment

        • Justin Koivisto

          #5
          Re: Login system

          frizzle wrote:[color=blue]
          > Thanks for the reply!
          >
          > Reading this, it seems to me, $_SERVER['REQUEST_URI'] isn't always set.
          > Isn't this a full server-side property?
          > I'm using mod_rewrite, so the second part of your code wouldn't be very
          > nice ... :([/color]

          It depends on the server software you are using. For instance, IIS
          doesn't have it:



          --
          Justin Koivisto, ZCE - justin@koivi.co m

          Comment

          • frizzle

            #6
            Re: Login system

            @Gordon
            I don't completely inderstand the next part:[color=blue]
            > "Well, if it's just a random link, it probably won't have your hidden
            > form-field with the place to go back to listed. isset($_PUT['go_back_to'])
            > might be useful to test this. "[/color]
            I understood that i should use the hidden field, but could you please
            try to explain (with other words) what you mean with that part?

            @Justin,
            I have the $_SERVER['REQUEST_URI'] implemented in some parts of the
            site already. And it always works AFAIK ...
            Could there be certain circumstances on my site that would have it to
            fail?

            Thanks a bunch guys!

            Frizzle.

            Comment

            • Justin Koivisto

              #7
              Re: Login system

              frizzle wrote:[color=blue]
              > @Gordon
              > I don't completely inderstand the next part:
              >[color=green]
              >>"Well, if it's just a random link, it probably won't have your hidden
              >>form-field with the place to go back to listed. isset($_PUT['go_back_to'])
              >>might be useful to test this. "[/color]
              >
              > I understood that i should use the hidden field, but could you please
              > try to explain (with other words) what you mean with that part?
              >
              > @Justin,
              > I have the $_SERVER['REQUEST_URI'] implemented in some parts of the
              > site already. And it always works AFAIK ...
              > Could there be certain circumstances on my site that would have it to
              > fail?[/color]

              Sure, if you move the site to a non-apache server... If you don't plan
              on moving it, you should be OK since that is an apache environment variable.

              --
              Justin Koivisto, ZCE - justin@koivi.co m

              Comment

              • frizzle

                #8
                Re: Login system

                I'm definetly not moving the site! :D

                Another thought: if the $_SERVER['http_referer'] is quite easy to fake,
                would a hidden field with $_SERVER['REQUEST_URI'] be even more easy to
                fake?
                I'm assuming the members WANT to login. Username & pass are checked
                from the DB, so if either referer, username or pass don't match, the
                user cannot login (as it is now). is there anything wrong with this?

                Now i'm also using the $_SERVER['http_referer'] for the logout action.
                It doesn't contain a form, but only requests a page that destroys some
                $_SESSION vars. This way i can send them back to the last page where
                they were logged in.

                Am i doing something wrong here then?

                Frizzle.

                Comment

                • Gordon Burditt

                  #9
                  Re: Login system

                  >@Gordon[color=blue]
                  >I don't completely inderstand the next part:[color=green]
                  >> "Well, if it's just a random link, it probably won't have your hidden
                  >> form-field with the place to go back to listed. isset($_PUT['go_back_to'])[/color][/color]

                  Oops. That should be isset($_POST['go_back_to']).
                  [color=blue][color=green]
                  >> might be useful to test this. "[/color]
                  >I understood that i should use the hidden field, but could you please
                  >try to explain (with other words) what you mean with that part?[/color]

                  If you have a hidden field in your login form, say:
                  <input type="hidden" name="go_back_t o" value="http://www.domain.com/foo.php">
                  (the value part will be generated from some variable like
                  $_SERVER['REQUEST_URI'])

                  on all of your login pages, then when the user clicks the SUBMIT button,
                  you will have "http://www.domain.com/foo.php" in $_POST['go_back_to'].
                  If someone else sets up a non-form link, $_POST['go_back_to'] will
                  be empty. A quick but unreliable way to test if one of your forms
                  was used is to test isset($_POST['go_back_to']). This should have
                  no false negatives (your login forms *ALWAYS* have the hidden field).
                  There might be false positives. So if that variable isn't set,
                  it DEFINITELY isn't one of your login forms.

                  Someone CAN copy the HTML for your login form and set it up on another
                  server.
                  [color=blue]
                  >@Justin,
                  >I have the $_SERVER['REQUEST_URI'] implemented in some parts of the
                  >site already. And it always works AFAIK ...
                  >Could there be certain circumstances on my site that would have it to
                  >fail?[/color]

                  Gordon L. Burditt

                  Comment

                  • Gordon Burditt

                    #10
                    Re: Login system

                    >Another thought: if the $_SERVER['http_referer'] is quite easy to fake,[color=blue]
                    >would a hidden field with $_SERVER['REQUEST_URI'] be even more easy to
                    >fake?[/color]

                    Probably not. Someone trying to DELIBERATELY fake is going to
                    succeed (without trying particularly hard). But many people running
                    security software, including those that block HTTP_REFERER, block
                    it without realizing it, and it may be darned inconvenient to NOT
                    block it. Also, something as simple as bookmarking a page and going
                    back to it will mess up HTTP_REFERER, and the user doing it may
                    have no idea why his login didn't work.

                    Remember, it is always possible for a user to (a) copy your HTML to
                    another server and edit it all he wants, or (b) manually type in
                    HTTP requests using telnet (or perhaps more conveniently, using CURL).
                    [color=blue]
                    >I'm assuming the members WANT to login. Username & pass are checked
                    >from the DB, so if either referer, username or pass don't match, the
                    >user cannot login (as it is now). is there anything wrong with this?[/color]

                    The referer stuff shouldn't be a *security* issue. As you described
                    it, it's a *convenience*. IMHO, if they give you an invalid referer
                    (remember, some users can't UNblock it), but a valid username and
                    password you should pick some reasonable default place to send them
                    (home page? ok. www.nambla.org? please don't.) after they've
                    logged in, and send them there. I think it's overly anal-retentive
                    to refuse a login here. It's much like not letting anyone with a
                    video display less than 65,000 colors and less than 20/20 vision
                    see your photographs AT ALL because they don't look their absolute
                    best, so you subject them to mandatory eye tests.

                    Why, incidentally, do you even want to refuse to send them back to
                    the page they logged in from if it's not yours? Sounds like
                    pretty obnoxious behavior.

                    [color=blue]
                    >Now i'm also using the $_SERVER['http_referer'] for the logout action.
                    >It doesn't contain a form, but only requests a page that destroys some
                    >$_SESSION vars. This way i can send them back to the last page where
                    >they were logged in.[/color]

                    If you can't send them back to the last page where they were logged
                    in because the URL looks invalid, pick some place to send them
                    and send them there. Your home page, perhaps.

                    Gordon L. Burditt

                    Comment

                    • frizzle

                      #11
                      Re: Login system

                      Ok, thanks for the explanation, this is quite what i had in mind as an
                      alternative.
                      Now i have only 1 question left: how do i handle the Logout-action.
                      Which is just started with a link, not a form. Should i then check for
                      a referer, if not set, send them to the home page?

                      What do you guys recon?

                      Thanks anyway, this has made me a lot more conficent about my
                      situation.

                      Frizzle.

                      Comment

                      • Gordon Burditt

                        #12
                        Re: Login system

                        >Ok, thanks for the explanation, this is quite what i had in mind as an[color=blue]
                        >alternative.
                        >Now i have only 1 question left: how do i handle the Logout-action.
                        >Which is just started with a link, not a form. Should i then check for
                        >a referer, if not set, send them to the home page?[/color]

                        Where do you want to send them after they log out? To the page
                        they came from when they logged *IN*? In that case, you take the
                        hidden form variable saved from when they logged in (saved in
                        $_SESSION, probably), and send them there. You can't use referer
                        here as they've likely been through several pages on your site while
                        logged in.

                        To the page where they clicked on LOGOUT? This doesn't seem very
                        logical to me. You could use referer for this, if it exists.
                        There's not a lot of incentive to fake it. If they want to go
                        elsewhere, they can click on HOME on the browser, or a bookmark,
                        or whatever.

                        Gordon L. Burditt

                        Comment

                        • frizzle

                          #13
                          Re: Login system

                          Why would you consider it unlogical to redir them to the page before
                          they logout?
                          My opinioin is that you should never redir users to pages they don't
                          expect, let them decide where they want to go ...

                          Why would you redir them to the page where they logged in?
                          They's might even 'not remember' they've been there ...

                          Frizzle.

                          Comment

                          • Gordon Burditt

                            #14
                            Re: Login system

                            >Why would you consider it unlogical to redir them to the page before[color=blue]
                            >they logout?[/color]

                            Because it is likely to contain meaningless or no data unless they
                            are logged in, as the page is indended to be used by logged-in users
                            and presumably has user-specific data on it. Also, they probably
                            aren't supposed to be able to get there if they are not logged in.
                            [color=blue]
                            >My opinioin is that you should never redir users to pages they don't
                            >expect, let them decide where they want to go ...
                            >
                            >Why would you redir them to the page where they logged in?
                            >They's might even 'not remember' they've been there ...[/color]

                            So they could log in again, possibly as a different user? You've
                            gotta send them *somewhere* and this is at least a page they've
                            been to before and it doesn't require them to be already logged in.

                            Gordon L. Burditt

                            Comment

                            • frizzle

                              #15
                              Re: Login system

                              Ok, thanks. I get your point.

                              There are pages that show EXTRA info for
                              logged-in people, other ones are only
                              accesable for logged people.

                              I'll probably make a page, telling
                              them the page is only viewable once
                              logged in. This way, it also looks better
                              when someone visits the page from the
                              browser's history...

                              Thanks for your help guys!

                              Frizzle.

                              Comment

                              Working...