1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help: (php) form/submit Button...

Discussion in 'PHP' started by Skillman13, Nov 18, 2009.

  1. #1
    I have this code that logs in a suer (If the username/password is found in a database)

    The code worked, until i added a hyperlink on the button...

    (I think this is because the button 'moves' you (redirects you) before it does the code before -check if username/password match database)

    Is there anyway to fix this? -get it to work with hyperlink?

    The code below may be completely wrong now...

    <form id="form1" name="form1" method="post" action="http://mountgame.com/zombie/main.php">
    <label></label>
    <label>
    <input name="textbox" type="text" id="textbox" value="" size="15" />
    </label>
    <input name="password" type="password" id="password" size="15" />
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </form>
    <?
    }
    if (isset($_POST['Submit'])) {

    $loggeduser = $_POST['textbox'];
    $loggedpass = $_POST['password'];
    $result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    $numrows= mysql_num_rows($result);

    if ($numrows == 0) {
    echo "Invalid username or password, please re-enter...";
    }


    Any working variation is good :)

    Thanks alot,

    James
     
    Skillman13, Nov 18, 2009 IP
  2. khajeya

    khajeya Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    where is hyperlink that you mean?

    just for your information, it will be better if you wrote your code like this:

    
    if (isset($_POST['Submit'])) {
    	$loggeduser = (!empty($_POST['textbox'])) ?  strip_tags(trim($_POST['textbox'])) : null;
    	$loggedpass = (!empty($_POST['password'])) ? strip_tags(trim($_POST['password'])) : null;
    	if(!empty($loggeduser) && !empty($loggedpass))
    	{
    		$result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    		$numrows= mysql_num_rows($result);
    		
    		if ($numrows == 0) {
    			echo "Invalid username or password, please re-enter...";
    		}
    		else
    		{
    			echo "VALID username and password...";
    		}
    	}
    	else
    	{
    		echo "Please enter your username and password!";
    	}
    }
    Code (markup):
     
    khajeya, Nov 18, 2009 IP
  3. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I mean the hyperlink (button)

    -The action="http://mountgame.com/zombie/main.php", it redirects you before doing the code =/ Is there a way to fix this?

    <form id="form1" name="form1" method="post" action="http://mountgame.com/zombie/main.php">
    <input name="textbox" type="text" id="textbox" value="" size="15" />
    <input name="password" type="password" id="password" size="15" />
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </form>
    <?
    }
    if (isset($_POST['Submit'])) {

    $loggeduser = $_POST['textbox'];
    $loggedpass = $_POST['password'];
    $result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    $numrows= mysql_num_rows($result);

    if ($numrows == 0) {
    echo "Invalid username or password, please re-enter...";
    }
    ?>
     
    Skillman13, Nov 18, 2009 IP
  4. bogdanUngureanu

    bogdanUngureanu Greenhorn

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    First of all SANITIZE YOUR VALUES THAT YOU WILL WORK WITH DATABASES!! you have a classic sql injection and you will be hacked...to sanitize the values, use mysql_real_escape_string() function.
    Try this code :
    
    <form id="form1" name="form1" method="post" action="http://mountgame.com/zombie/main.php">
    <input name="textbox" type="text" id="textbox" value="" size="15" />
    <input name="password" type="password" id="password" size="15" />
    <input name="do_insert" type="hidden" value="TRUE"  />
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </form>
    <?
    }
    if (!empty($_POST['do_insert'])) {
    
    $loggeduser = mysql_real_escape_string($_POST['textbox']);
    $loggedpass = mysql_real_escape_string($_POST['password']);
    $result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    $numrows= mysql_num_rows($result);
    
    if ($numrows == 0) {
    echo "Invalid username or password, please re-enter...";
    }
    ?> 
    
    Code (markup):
     
    bogdanUngureanu, Nov 18, 2009 IP
  5. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, i have no idea what that does, but still the code does not work after =/ -the php code after clicking the form button.

    Can anyone fix this? :)
     
    Skillman13, Nov 18, 2009 IP
  6. bogdanUngureanu

    bogdanUngureanu Greenhorn

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #6
    What do you mean when do you say "doesn't work"? what does appear after do you click the submit button?
     
    bogdanUngureanu, Nov 18, 2009 IP
  7. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Nothing at all, it just takes you to the /main.php page with no $_SESSSION['username']
     
    Skillman13, Nov 18, 2009 IP
  8. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I want it to run the php code; then take you to /main.php, =/
     
    Skillman13, Nov 18, 2009 IP
  9. bogdanUngureanu

    bogdanUngureanu Greenhorn

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #9
    man, that script that you posted is NOT main.php?
     
    bogdanUngureanu, Nov 19, 2009 IP
  10. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    No its a login form on index.php, i want it to redirect you onto main.php, so can you fix it?

    or if you know a better way? :) -like keep it on the same page but make the textbox/button go invisible...
     
    Skillman13, Nov 19, 2009 IP
  11. bogdanUngureanu

    bogdanUngureanu Greenhorn

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #11
    
    <form id="form1" name="form1" method="post" action="index.php">
    <input name="textbox" type="text" id="textbox" value="" size="15" />
    <input name="password" type="password" id="password" size="15" />
    <input name="do_insert" type="hidden" value="TRUE"  />
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </form>
    <?
    }
    if (!empty($_POST['do_insert'])) {
    
    $loggeduser = mysql_real_escape_string($_POST['textbox']);
    $loggedpass = mysql_real_escape_string($_POST['password']);
    $result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    $numrows= mysql_num_rows($result);
    
    if ($numrows == 0) {
    echo "Invalid username or password, please re-enter...";
    }
    else{
    session_start();
    $row = msql_fetch_array($result);
    
    $_SESSION['username'] = $row['username'];
    $_SESSION['password'] = $row['password'];
    
    header("location: main.php");
    
    }
    
    }
    ?>
    
    Code (markup):
    I haven't tested it, but I think should work...
    Attention, this is not a good ideea for a login sistem!!!! you must verify on EVERY page if the $_session['username'] and $_session['password'] are correct.
    And another thing is that is not a good idea to save the password not crypted in the database. To encrypt the password, use sha1() function.
     
    bogdanUngureanu, Nov 19, 2009 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Then you should not put main.php as the form action. Validate the form in index.php first and redirect users to main.php when its done.

    Like the post above.
     
    ads2help, Nov 19, 2009 IP
  13. cignusweb

    cignusweb Peon

    Messages:
    147
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    my be you are not put main.php as the form action. Validate the form in index.php first and redirect users to main.php when its done.
     
    cignusweb, Nov 19, 2009 IP
  14. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ah thanks ok, i never knew a redirection worked -Or never knew there was such thing. And yeah this is a basic login. I'll encrypt data and verify _SESSION user/pass on each page when i finish the rest of the site.
     
    Skillman13, Nov 19, 2009 IP
  15. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hmmm, appears not to be working...

    Warning: Cannot modify header information - headers already sent by (output started at /home/mountgam/public_html/zombie/index.php:9) in /home/mountgam/public_html/zombie/index.php on line 34

    =/ Any fixes? :)
     
    Skillman13, Nov 19, 2009 IP
  16. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Works now -I think...
    <? ob_start(); ?> Helps =/ :)
     
    Skillman13, Nov 19, 2009 IP
  17. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Damn, another problem... not working now... =/ Any other fixes?
     
    Skillman13, Nov 19, 2009 IP
  18. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0

    =/ so im not using the ob_start();

    Any fixes for header ("location: example.php") after a html bit/form?
     
    Skillman13, Nov 19, 2009 IP
  19. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Anyone? =/
     
    Skillman13, Nov 19, 2009 IP
  20. Thiagoo

    Thiagoo Peon

    Messages:
    441
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Ok, i havent read all the thread but it was really easy to spot what you did wrong, i doubt anyone that didnt tell you it earlier dont know shit about php.

    This code:

    <?
    }
    if (!empty($_POST['do_insert'])) {

    $loggeduser = mysql_real_escape_string($_POST['textbox']);
    $loggedpass = mysql_real_escape_string($_POST['password']);
    $result = mysql_query("SELECT * FROM `Zombie` WHERE `Username` = '$loggeduser' AND `Password` = '$loggedpass'");
    $numrows= mysql_num_rows($result);

    if ($numrows == 0) {
    echo "Invalid username or password, please re-enter...";
    }
    ?>

    Should be on this page:

    http://mountgame.com/zombie/main.php


    That's all.
     
    Thiagoo, Nov 19, 2009 IP