PHP Password And Redirect Script...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eragon
    Contributor
    • Mar 2007
    • 431

    PHP Password And Redirect Script...

    Hello peoples. I have a voting system on my web site that uses 3 files. The first file is vote.php and has the vote form on it. This file passes the information to confirmation.ph p which has a thank you message on it. This file passes the information to vote.txt, which stores the information for me to tally up later. What i described to you works great, but theres one problem. I want users to have to log in first, using a login form on voteli.php. I have login scripts, but theyre not even close to secure. I want it so if they get smart and go directly to vote.php it will kick them back to voteli.php unless varaible $login is true. I would also like the login script to pass the username through all the pages as varaible $user. Then i wil echo the varaible in the page so it has a greeting message. I will also use this name in the results page to cancel out any double votes by the same user. I will provide the source codes i made using my small knowledge of php, and i will hope that you suggest some fixes.

    vote.php:
    [PHP]<?php
    $login = $_GET['login'];
    $user = $_GET['username']

    if(!$login)
    {
    window.location =('voteli.htm') ;
    }

    else
    {
    echo('Hello, '$user'!');
    }
    ?>[/PHP]The code above is the one i wrote to bounce the users back to the login page if they have not logged in. It also prints the hello message at the top of the vote page.

    [HTML]
    <?php echo('Voting as: '$user'.') ?>
    <form name="poll" action="confirm ation.php" method="get">
    Lorem ipsum sit dolor amet?
    <select name="q1">
    <option value="q1a">Lor em</option>
    <option value="q1b">Ips um</option>
    <option value="q1c">Dol or</option>
    <option value="q1d">Ame t</option>
    </select>
    </form>[/HTML]
    The code above is the simplified vote form.

    confirmation.ph p
    [HTML]<?php
    $q1 = $_GET['q1'];
    //See notes below for username problem.

    $filename = "vote.txt";
    $content1 = "$v1\n";
    $fp = fopen($filename , "a");
    $fw = fwrite( $fp, $content1 );
    fclose( $fp );

    ?>[/HTML]Tha code above writes to the text file. I need a way to get the username passed from vote.php to confirmation.ph p so i can display the name in the thanks message.

    [HTML]Thank you for voting, <?php echo($user); ?>! Your vote will count. <br />
    <!-- A logout link would be nice -->Click Here to log out.[/HTML]
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    First: Use POST instead of GET as your FORM METHOD. Check the Referer to ensure that the POST came from your server.
    Second: You cannot do window.location as that is Javascript langauge for sending a user places. Instead, you want to use header('Locatio n: voteli.htm');
    Third: You will most likely want to check that the POST array is set before you go assigning their values to other variables.
    Fourth: Have you performed any credential checking? You have not compared user and login to anything to ensure that these are the correct credentials. You should also ensure that the data coming from your select box is valid.

    I am sure there is more, but this is a start. If you really wanted to be fancy, you could combine the whole set into one PHP page.

    Comment

    • eragon
      Contributor
      • Mar 2007
      • 431

      #3
      ill use post. i know window.location is javascript i was just putting that there cause i knew somebody would suggest a change, and thank you for that. and ill doo all that other stuff and see. also, i need the whole login script. i dont have one that works with php.

      Comment

      • eragon
        Contributor
        • Mar 2007
        • 431

        #4
        ok, i know what i need. i need a login script that passes the following information to the next page ONLY if the user and pass is correct:

        Input to script:
        Code:
        Username: Admin
        Password: password
        Output to vote.php:
        Code:
        $login=('true')
        $user=('admin')
        Possible Form:
        [HTML]<form method="post" action="confirm ation.php">
        Username:<input name="user" type="text"><br />
        Password:<input name="pass" type="password" >
        </form>[/HTML]

        What I need:
        I need a php script to work with the above form to validate the usernames and passwords when the form is submitted. If the information does not check out to be valid, it should put "Invalid Username or Password" in a DIV tag. i also need the correct syntax for the below statements:
        [PHP]<?php
        if $login=('true')
        {
        //this would bypass the script (end, exit, , or goto... ?)
        }
        else
        {
        header('Locatio n: voteli.htm');
        }
        ?>[/PHP]

        If you need more information just ask.

        Comment

        • eragon
          Contributor
          • Mar 2007
          • 431

          #5
          Also, if i was to incoorperate all this into a MySQL database (usernames, passwords, and what that user voted for) i would need another script. Could somebody please suggest sometihng to me??

          Comment

          • eragon
            Contributor
            • Mar 2007
            • 431

            #6
            one wheel said to the other, "Ill see you around, ay?"

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              [code=php]
              <html>
              <head><title>Mo toma Rocks My Homework</title></head>
              <body>
              <?php

              session_start() ;

              if(!isset($_SES SION['userid']))
              {
              if(!isset($_POS T['username']) || !isset($_POST['password']))
              {
              echo '<form method="POST">< input type="text" name="username" /><input type="password" name="password" /><input type="submit" /></form>';
              }
              else if(authenticate ($_POST['username'], $_POST['password']))
              {
              $_SESSION['userid'] = $_POST['username'];
              header('Locatio n: thispage.php');
              }
              }
              else
              {
              if(!isset($_POS T['data1']) || !isset($_POST['data2'])) //fill with all question data objects
              {
              echo '<form method="POST">< select name="data1"><o ption value="q1a">Lor em</option><option value="q1b">Ips um</option>
              </select><select name="data2"><o ption value="q1a">Lor em</option><option value="q1b">Ips um</option></select></form>';
              }
              else
              {
              AppendDataToFil e($_SESSION['userid'], $_POST['data1'], $_POST['data2']);
              echo 'Thank you for your submission '.$_SESSION['userid'].'!';
              }
              }

              function authenticate($u , $p)
              {
              if($u == 'admin' && $p == 'password') return true; //replace with the actual authentication.
              return false;
              }
              ?>
              </body>
              </html>
              [/code]

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by eragon
                Also, if i was to incoorperate all this into a MySQL database (usernames, passwords, and what that user voted for) i would need another script. Could somebody please suggest sometihng to me??
                If you take a look at the PHP Articles (under the Articles Header) you will see a tutorial labeled " Creating a Data Abstraction Layer in PHP" which would be a great start.

                Comment

                • eragon
                  Contributor
                  • Mar 2007
                  • 431

                  #9
                  thanks

                  ..............

                  Comment

                  • eragon
                    Contributor
                    • Mar 2007
                    • 431

                    #10
                    oh, ya, and i had vote.php seperate because of one little problem... the voting page has:
                    A) 15 questions
                    B) Is made up of pictures, tables, divs, and more...
                    C) If i was to tell a php script to echo the WHOLE source code, php will jump out and bite me.
                    4) I think i can customise the script. im a very fast learner.

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      Well, good luck. Come back if you have more questions.

                      Comment

                      • eragon
                        Contributor
                        • Mar 2007
                        • 431

                        #12
                        ok, for multiple users and passwords, would i duplicate the line like this:

                        [PHP]{
                        if($u == 'admin' && $p == 'password') return true;
                        if($u == 'admin2' && $p == 'authentication ') return true;
                        if($u == 'admin3' && $p == 'fish') return true;

                        return false;
                        }[/PHP]

                        Comment

                        • eragon
                          Contributor
                          • Mar 2007
                          • 431

                          #13
                          Error, error! Red alert! Emergency! Ah-wooo-ga!

                          what caused this? (using the exact code you sent me)
                          Code:
                          Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/votealt.php:4) in /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/votealt.php on line 6
                          
                          Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/votealt.php:4) in /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/votealt.php on line 6

                          Comment

                          • eragon
                            Contributor
                            • Mar 2007
                            • 431

                            #14
                            Originally posted by Motoma
                            Well, good luck. Come back if you have more questions.

                            ^^^^^ see above post ^^^^^

                            Comment

                            • Motoma
                              Recognized Expert Specialist
                              • Jan 2007
                              • 3236

                              #15
                              Bah, you will have to put session_start() before any HTML.

                              Comment

                              Working...