• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Mein Code

MBaaboura

Neues Mitglied
Hallo HTML.de Community!

Es tut mir leid wenn das hier im falschen Forum gepostet ist ,aber ich würde gerne wissen ob dieser Code gut geschrieben ist:


PHP:
<?php
session_start(); // Start Session First Thing
// Force script errors and warnings to show on page in case php.ini file is set to not display them
error_reporting(E_ALL);
ini_set('display_errors', '1');
//-----------------------------------------------------------------------------------------------------------------------------------
include_once "connect_to_mysql.php"; // Connect to the database
$dyn_www = $_SERVER['HTTP_HOST']; // Dynamic www.domainName available now to you in all of your scripts that include this file
//------ CHECK IF THE USER IS LOGGED IN OR NOT AND GIVE APPROPRIATE OUTPUT -------
$logOptions = ''; // Initialize the logOptions variable that gets printed to the page
// If the session variable and cookie variable are not set this code runs
if (!isset($_SESSION['idx'])) { 
  if (!isset($_COOKIE['idCookie'])) {
     $logOptions = '<a href="http://de.bigfoxx.hostingsociety.com/register.php">Register Account</a>
     &nbsp;&nbsp; | &nbsp;&nbsp; 
     <a href="http://de.bigfoxx.hostingsociety.com/login.php">Log In</a>';
   }
}
// If session ID is set for logged in user without cookies remember me feature set
if (isset($_SESSION['idx'])) { 
    
    $decryptedID = base64_decode($_SESSION['idx']);
    $id_array = explode("p3h9xfn8sq03hs2234", $decryptedID);
    $logOptions_id = $id_array[1];
    //$logOptions_username = $_SESSION['username'];
    //$logOptions_username = substr('' . $logOptions_username . '', 0, 15); // cut user name down in length if too long
    
    // Check if this user has any new PMs and construct which envelope to show
    $sql_pm_check = mysql_query("SELECT id FROM private_messages WHERE to_id='$logOptions_id' AND opened='0' LIMIT 1");
    $num_new_pm = mysql_num_rows($sql_pm_check);
    if ($num_new_pm > 0) {
        $PM_envelope = '<a href="pm_inbox.php"><img src="../images/pm2.gif" width="18" height="11" alt="PM" border="0"/></a>';
    } else {
        $PM_envelope = '<a href="pm_inbox.php"><img src="../images/pm1.gif" width="18" height="11" alt="PM" border="0"/></a>';
    }
    // Ready the output for this logged in user
    $logOptions = $PM_envelope . ' &nbsp; &nbsp;
    <!--<a href="http://' . $dyn_www . '">Home</a>
    &nbsp;&nbsp; |&nbsp;&nbsp; -->
    <a href="http://' . $dyn_www . '/profile.php?id=' . $logOptions_id . '">Profile</a>
    &nbsp;&nbsp; |&nbsp;&nbsp;
    <div class="dc">
<a href="#" onclick="return false">Account &nbsp; <img src="../images/darr.gif" width="10" height="5" alt="Account Options" border="0"/></a>
<ul>
<li><a href="http://de.bigfoxx.hostingsociety.com/edit_profile.php">Account Options</a></li>
<li><a href="http://de.bigfoxx.hostingsociety.com/pm_inbox.php">Inbox Messages</a></li>
<li><a href="http://de.bigfoxx.hostingsociety.com/pm_sentbox.php">Sent Messages</a></li>
<li><a href="http://de.bigfoxx.hostingsociety.com/logout.php">Log Out</a></li>
</ul>
</div>
';

} else if (isset($_COOKIE['idCookie'])) {// If id cookie is set, but no session ID is set yet, we set it below and update stuff
    
    $decryptedID = base64_decode($_COOKIE['idCookie']);
    $id_array = explode("nm2c0c4y3dn3727553", $decryptedID);
    $userID = $id_array[1]; 
    $userPass = $_COOKIE['passCookie'];
    // Get their user first name to set into session var
    $sql_uname = mysql_query("SELECT username, email FROM myMembers WHERE id='$userID' AND password='$userPass' LIMIT 1");
    $numRows = mysql_num_rows($sql_uname);
    if ($numRows == 0) {
        // Kill their cookies and send them back to homepage if they have cookie set but are not a member any longer
        setcookie("idCookie", '', time()-42000, '/');
        setcookie("passCookie", '', time()-42000, '/');
        header("location: index.php"); // << makes the script send them to any page we set
        exit();
    }
    while($row = mysql_fetch_array($sql_uname)){ 
        $username = $row["username"];
        $useremail = $row["email"];
    }

    $_SESSION['id'] = $userID; // now add the value we need to the session variable
    $_SESSION['idx'] = base64_encode("g4p3h9xfn8sq03hs2234$userID");
    $_SESSION['username'] = $username;
    $_SESSION['useremail'] = $useremail;
    $_SESSION['userpass'] = $userPass;

    $logOptions_id = $userID;
    //$logOptions_uname = $username;
    //$logOptions_uname = substr('' . $logOptions_uname . '', 0, 15); 
    ///////////          Update Last Login Date Field       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    mysql_query("UPDATE myMembers SET last_log_date=now() WHERE id='$logOptions_id'"); 
    // Ready the output for this logged in user
    // Check if this user has any new PMs and construct which envelope to show
    $sql_pm_check = mysql_query("SELECT id FROM private_messages WHERE to_id='$logOptions_id' AND opened='0' LIMIT 1");
    $num_new_pm = mysql_num_rows($sql_pm_check);
    if ($num_new_pm > 0) {
        $PM_envelope = '<a href="pm_inbox.php"><img src="../images/pm2.gif" width="18" height="11" alt="PM" border="0"/></a>';
    } else {
        $PM_envelope = '<a href="pm_inbox.php"><img src="../images/pm1.gif" width="18" height="11" alt="PM" border="0"/></a>';
    }
    // Ready the output for this logged in user
     $logOptions = $PM_envelope . ' &nbsp; &nbsp;
     <!--<a href="http://' . $dyn_www . '">Home</a>
    &nbsp;&nbsp; |&nbsp;&nbsp; -->
     <a href="http://' . $dyn_www . '/profile.php?id=' . $logOptions_id . '">Profile</a>
    &nbsp;&nbsp; |&nbsp;&nbsp;
    <div class="dc">
<a href="#" onclick="return false">Account &nbsp; <img src="../images/darr.gif" width="10" height="5" alt="Account Options" border="0"/></a>
<ul>
<li><a href="http://' . $dyn_www . '/edit_profile.php">Account Options</a></li>
<li><a href="http://' . $dyn_www . '/pm_inbox.php">Inbox Messages</a></li>
<li><a href="http://' . $dyn_www . '/pm_sentbox.php">Sent Messages</a></li>
<li><a href="http://' . $dyn_www . '/logout.php">Log Out</a></li>
</ul>
</div>';
}
?>

Wenn ihr Fehler findet postet bitte.
 
„boblean“ steht da garantiert nicht. Das mag an der Stelle hier spitzfindig sein, aber beim Programmieren sind es nun mal häufig solche Details.

Gehst du auf den Rest meines Posts auch noch ein?
 
In deinem Post (#5) steht „boblean“, obwohl das „boolean“ heißen müsste. Keine Ahnung wieso.

Mach aber mal bitte das, was ich in Post #6 empfohlen habe. Die Query ist fehlerhaft.
 
Zunächst sollte mal zwischen Gut und Fehlerfrei unterschieden werden. Da einmal nach dem und dann wieder nach dem anderen gefragt wurde.

Kommen wir zu Gut was stellst du dir darunter vor. Das es einer Profi Programmierung entspricht? Wenn ja hier muss ich die entäuschen es ist ein simples Anfänger Script. Das eben die nötigsten Grundlagen zeigt.

Vielleicht meinst du aber auch mit Gut die Sicherheit des Scriptes. Oder die wiederverwendbarkeit.


Nun zu Fehlerfrei:
100% sicherheit wirst du nie sagen können das ein Script fehlerfrei ist. Den Laufzeitfehler lassen sich nur sehr schwer lokalisieren und können eben auch sporatisch auftauchen. Dazu wurden Unitest entwickelt die das Script auf solche Fehler hin prüfen soll. Daher schreibt man Subroutienen die das eigenentliche Script testen.


Zusammenfassung:


Zum Lehrnen und die ersten Schritte in Php zu machen wird das gepostet Script hier sicherlich ausreichen. Aber du solltes es auch verstehen einfach nur abschreiben aus einen Tutorial bring wenig bis nix.

Weiters solltes du auch erwähnen aus welchen Tutorial du das ganze kopiert hast. ( Link )
Um entuell auch die vom Autor vorstellungen nachvollziehen zu können. Was wollte er machen und was hat er gemacht.



Mfg Splasch
 
Zuletzt bearbeitet:
Jetzt steht da nicht mehr boolean aber immernoch die Fehlermeldung.
Du musst dich ganz dringend mit Mysql beschäftigen :)
Denn deine Query schlägt eindeutig fehl. Wenn der Wert, der bei der query raus kommt false ist (false ist ein boolean), hat das ganze nicht funktioniert.
Damit wir weiter kommen müsstest du dein Script mal anpassen. Hier wäre mal eine Übergangslösung:

PHP:
// Check if this user has any new PMs and construct which envelope to show
    $sql_pm_check = mysql_query("SELECT id FROM private_messages WHERE to_id='$logOptions_id' AND opened='0' LIMIT 1");
    if($sql_pm_check === false) {
       echo "FEHLER: ".mysql_errno()." : ".mysql_error();
       $num_new_pm = 0;
    } else {
      $num_new_pm = mysql_num_rows($sql_pm_check);
    }
 
Sorry wenn das jetzt dumm ist ,aber ihr postet immer so komische Skripte mit Sachen von wegen Private Message und so.
Einmal bitte genau diese Zeile in den von dir geposteten Code suchen.... -_-
Irgendwie Schade, dass du dich so wenig mit dem Code beschäftigt hast, dass du Code-Schnipsel daraus nicht mehr wieder erkennst.
 
Ok..

Ich habe auch einen Fehler hier erkannt den ich nicht finden kann..

PHP:
<?php
include_once "connect_to_mysql.php"; // <<---- Connect to database here$username = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']); // filter$sql_uname_check = mysql_query("SELECT id FROM myMembers WHERE username='$username' LIMIT 1"); $uname_check = mysql_num_rows($sql_uname_check);
if (strlen($username) < 4) {    echo '<span style="background: #FDD; border:#900 1px solid; padding:4px;">Der Username muss 4 - 20 Zeichen lang sein!</span>';    exit();}
if ($username == "" || $username == " ") {    echo '<span style="background: #FDD; border:#900 1px solid; padding:4px;">Nicht verfügbar. Bitte versuche es erneut!</span>';    exit();}
if ($uname_check < 1) {    echo '<span style="background:#CEFFCE; border:#060 1px solid; padding:4px;"><strong>' . $username . '</strong> ist verfügbar!</span>';    exit();} else {    echo '<span style="background: #FDD; border:#900 1px solid; padding:4px;"><strong>' . $username . '</strong> ist bereits vergeben</span>';    exit();}?>
 
Zurück
Oben