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.

What is wrong with this PHP Code?

Discussion in 'PHP' started by xbernx, Oct 20, 2015.

  1. #1
    Hi i have following code

    <?php
    
      $mysql_server = 'Database Server';
      $mysql_username = 'Username';
      $mysql_password = 'Password';
      $mysql_dbname = 'Database Name';
    
      $connection = mysql_connect($mysql_server, $mysql_username) or die("Verbindung zur Datenbank konnte nicht aufgebaut werden!");
     
      mysql_select_db($mysql_dbname, $connection) or die("Datenbank konnte nicht ausgewählt werden");
    
    $username = $_POST["username"];
    $passwort = $_POST["passwort"];
    $passwort2 = $_POST["passwort2"];
    
    if($passwort != $passwort2 OR $username == "" OR $passwort == "")
        {
        echo "Eingabefehler. Bitte alle Felder korekt ausfüllen. <a href=\"eintragen.html\">Zurück</a>";
        exit;
        }
        }
    $passwort = md5($passwort);
    
    $result = mysql_query("SELECT id FROM login WHERE username LIKE '$username'");
    $menge = mysql_num_rows($result);
    
    if($menge == 0)
        {
        $eintrag = "INSERT INTO login (username, passwort) VALUES ('$username', '$passwort')";
        $eintragen = mysql_query($eintrag);
    
        if($eintragen == true)
            {
            echo "Benutzername <b>$username</b> wurde erstellt. <a href=\"login.html\">Login</a>";
            }
        else
            {
            echo "Fehler beim Speichern des Benutzernames. <a href=\"eintragen.html\">Zurück</a>";
            }
    
    
        }
    
    else
        {
        echo "Benutzername schon vorhanden. <a href=\"eintragen.html\">Zurück</a>";
        }
    ?>
    PHP:
    Everytime i load the .html document with the form for registratiion and input mydetails and press send it can't connect to the mxsql database.
    What's wrong?
     
    xbernx, Oct 20, 2015 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,996
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    Apart from the fact you're using outdated mysql functions instead of PDO or mysqli and you have no security in there whatsoever, You're not passing the mysql password to the db connection.
     
    malky66, Oct 20, 2015 IP
    Arick unirow, deathshadow and ryan_uk like this.
  3. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #3
    I would have said that your trying to connect to nothing.

    $mysql_server = 'Database Server';

    I'd assume it should be if this were 10 years ago

    $mysql_server = 'localhost';

    however like
    @malky66 said, you should be using PDO or mysqli.

    This code is checking if a user exists and then it creates the user if not.

    Also are you saying that you have a file .html and threw PHP code into it? Why not just save it as a .php file?

    I would say the better question to ask here is what is "right" about this code? because so much is wrong with this that it should be scrapped.
     
    KangBroke, Oct 21, 2015 IP
  4. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #4

    If you double checked you would see that the OP is using MD5 when creating the account, Not saying this is the way to go, but you said no security.

    If you take the advice of Malky66 what you would need to do is change this line

    $connection = mysql_connect($mysql_server, $mysql_username) or die("Verbindung zur Datenbank konnte nicht aufgebaut werden!");

    to

    $connection = mysql_connect($mysql_server, $mysql_username, $mysql_password) or die("Verbindung zur Datenbank konnte nicht aufgebaut werden!");


    However its hard to even say that, just learn how to use mysqli it is not much different from mysql so it should be easy enough to understand.
     
    KangBroke, Oct 21, 2015 IP
  5. malky66

    malky66 Acclaimed Member

    Messages:
    3,996
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #5
    Huh?...MD5 is not going to cure the sql injection vulnerabilities on his unsanitized POST variables though is it?
    That's the lack of security I was referring to.
     
    malky66, Oct 21, 2015 IP
    ryan_uk likes this.
  6. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #6

    You never said lack, You said none, I was saying there was some just not the best. Its like he left his front door wide open but closed the screen door. Im not saying that screen door will keep out burglars but at least it is there.

    Like I did state "so much is wrong with this that it should be scrapped."
     
    KangBroke, Oct 21, 2015 IP
  7. malky66

    malky66 Acclaimed Member

    Messages:
    3,996
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #7
    Yeah, I know what I said, Like I said in my previous post I was referring to the unsanitized POST variables of which there is NO security.
    You want to carry on with your pointless pedantic bullshit argument...I'll leave you to it cause' I ain't interested.
     
    malky66, Oct 21, 2015 IP
    Arick unirow and ryan_uk like this.
  8. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #8
    Sorry didn't realize it was your time of the month, I was not arguing with you. Just pointing things out. Sorry I hurt your feelings.
     
    KangBroke, Oct 21, 2015 IP
    ThePHPMaster likes this.
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    You see this?

    $connection = mysql_connect
    Code (markup):
    YOU SEE THIS?

     $eintrag = "INSERT INTO login (username, passwort) VALUES ('$username', '$passwort')";
    Code (markup):
    @malky66 doesn't have to apologize for **** or be corrected. ZERO ***ing security thanks to the bleeding edge of DECADE old buggy insecure practices.
     
    deathshadow, Oct 21, 2015 IP
    malky66 and Arick unirow like this.
  10. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #10
    How did this turn into you guys trying to come at me? I never once said anything about Malky66 having to apologize, I just pointed out that the OP did use MD5 instead of just storing the password. So I am in the wrong for that? You guys must be friends and I must have really hurt his feelings. I also said more than once that this code should be thrown away. I just simply explained how the OP could make this crappy code work. I don't really understand how that warranted your response directed at me at all. Your post isn't even slightly contributing to the thread other than to bash me, and that is not the norm for you. Actually shocked you of all people came at me in that manner seeing you speak in a professional manner in so many other threads.
     
    KangBroke, Oct 21, 2015 IP