Forcing Code To Work !

Started by
26 comments, last by tanzanite7 10 years, 3 months ago

Whenever I am doing a project, I have a tendency to add all the features I want, than some how force everything to work together. Later I go back to streamline the code ( sometimes even I have problems reading it ).

This often times produces interesting "raw" code.

I present to you, the raw code I produced for a login page ( and it all works ) !


<html>
<title>Landing Page</title>
<head></head>
<?php
if (isset($_COOKIE["a"])){
	if (file_exists("u/".$_COOKIE["a"]) ){
	$co = file_get_contents("u/".$_COOKIE["a"]);
	$co2 = explode("|", $co);
	echo "Hello: " . $co2[0] . "<br>";
	}
	if (!file_exists("u/".$_COOKIE["a"]) ){
	unset($_COOKIE["a"]);
	}
}
 if (isset($_POST["su"]) ){
  echo "Make sure you fill in everything, and you use more than 4 characters.<br>";
  echo '
  <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
    Game Name: <input type="text" name="gn" maxlength="12"><br>
	User Name: <input type="text" name="un" maxlength="12"><br>
    Password : <input type="text" name="pw" maxlength="12"><br>
	<button type="submit">Submit !</button><br>
  </form>
  ';
 }
 else {
 if (isset($_POST["gn"]) && isset($_POST["un"]) && isset($_POST["pw"]) ){
  if ( trim($_POST["gn"])!= "" && trim($_POST["un"])!= "" && trim($_POST["pw"])!= "" && strlen($_POST["gn"]) > 4 && strlen($_POST["un"]) > 4 && strlen($_POST["pw"]) > 4 ) {
  $_POST["si"] = " bla ";
  if (!file_exists("p") ){
   mkdir("p", 0, true);
  }
  if (file_exists("p/".$_POST["gn"]) ){
   echo 'Name Taken</br>
   <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
  <input type="submit" name="su" value="Sign Up">
  </form>
   ';
  }
  elseif(!file_exists("p/".$_POST["un"]) ){
  $x = "1234567890abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $x2 = '';
 for ($i = 0; $i < 21; $i++) {
      $x2 .= $x[rand(0, strlen($x) - 1)];
	  }
   $fh = fopen("p/".$_POST["un"], 'w');
   // game name | password | user ID
   fwrite($fh,$_POST["gn"]."|".$_POST["pw"]."|".$x2);
   fclose($fh); 
   if (!file_exists("u") ){
   mkdir("u", 0, true);
   }
   $fh = fopen("u/".$x2, 'w');
   fwrite($fh,$_POST["gn"]."|". "This is were game stats go");
   fclose($fh); 
   unset($_POST["su"]);
   unset($_POST["gn"]);
   unset($_POST["un"]);
   unset($_POST["pw"]);
   echo 'Account created: Please log in.<br>
   <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
  <input type="submit" name="x" value="Log In">
  </form>
   ';
 }}
  else{
  $_POST["su"] = " bla ";
  echo '<center><h1>Information Rejected - Please Try Again !</h1><br> Do not resend information.<br></center>
    <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
  <input type="submit" name="su" value="Sign Up">
  </form>
  ';
  }
 }
 if (isset($_POST["a"]) && isset($_POST["b"]) ){
    
	if (file_exists("p/".$_POST["a"]) && trim($_POST["a"] != "") ){
	$uf = file_get_contents("p/".$_POST["a"]);
	// game name | password | user ID
	$uf2 = explode("|", $uf);
	if ($uf2[1] == trim($_POST["b"])){
	setcookie("a", $uf2[2], time()+3600);
	echo 'Welcome: '. $uf2[0]. '
	<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
    <input type="submit" name="x" value=" Not Programmed In Yet ">
    </form>
	';
	}
	elseif ($uf2[1] != trim($_POST["b"])){
	echo 'Nope!<br>
	<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
    <input type="submit" name="x" value=" <-- Back ">
    </form>
	';
	}
	}
	elseif (!file_exists("p/".$_POST["a"] || trim($_POST["a"] == "")) ){
	unset($_POST["a"]);
	unset($_POST["b"]);
	echo 'Does Not Exist<br>
  <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
  <input type="submit" name="x" value=" <-- Back ">
  </form>
	';
	}
    }
elseif (!isset($_POST["su"]) && !isset($_POST["si"]) ) {
 echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST" onsubmit=" ">
<br>
  User Name: <input type="text" name="a" maxlength="12">
  Password: <input type="text" name="b" maxlength="12">
  <button type="submit">Submit !</button>
  </form><br>
  <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
  <input type="submit" name="su" value="Sign Up">
  </form>
  ';
   }
 }
 
 ?>
</html>

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Advertisement

Mph. That made me uncomfortable but I guess it's just PHP? :P

Previously "Krohm"

You do know you can close your php tags and re-open them so you don't have to worry about echo-ing mark-up, right?


<?php
if(something)
{
?>
<p>Printing this because of <?php echo $valueOfSomething; ?></p>
<?php
} else
{
?>
<p>Printing this as our else paragraph.</p>
<?php
}
?>

You do know you can close your php tags and re-open them so you don't have to worry about echo-ing mark-up, right?

All the forms and text fields on the page are only to appear if a certain $_POST or $_COOKIE condition is true tongue.png


if (isset($_POST["su"]) ){
  echo "Make sure you fill in everything, and you use more than 4 characters.<br>";
  echo '
  <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
    Game Name: <input type="text" name="gn" maxlength="12"><br>
	User Name: <input type="text" name="un" maxlength="12"><br>
    Password : <input type="text" name="pw" maxlength="12"><br>
	<button type="submit">Submit !</button><br>
  </form>
  ';
 }

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

o god, ur transmitting and storing passwords as plaintext. screw bad code, thats just bad.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

if (file_exists("u/".$_COOKIE["a"]) ){
	$co = file_get_contents("u/".$_COOKIE["a"]);
	$co2 = explode("|", $co);
	echo "Hello: " . $co2[0] . "<br>";
	}
	if (!file_exists("u/".$_COOKIE["a"]) ){
	unset($_COOKIE["a"]);
	}

If exists.

If !exists.laugh.png

Woah, file_get_contents as well as fopen+fwrite on user-supplied, non-verified inputs. That's courageous.

You do know you can close your php tags and re-open them so you don't have to worry about echo-ing mark-up, right?

All the forms and text fields on the page are only to appear if a certain $_POST or $_COOKIE condition is true tongue.png


if (isset($_POST["su"]) ){
  echo "Make sure you fill in everything, and you use more than 4 characters.<br>";
  echo '
  <form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
    Game Name: <input type="text" name="gn" maxlength="12"><br>
	User Name: <input type="text" name="un" maxlength="12"><br>
    Password : <input type="text" name="pw" maxlength="12"><br>
	<button type="submit">Submit !</button><br>
  </form>
  ';
 }

That's what I am talking about. You can close the php tag after the opening if brace ( { ) and re-open the php tag after. That way you are not worrying about echo commands, which quotes to use or escape, etc. It will still be within the conditional and only be printed/sent to the client if that condition is true.


if (file_exists("u/".$_COOKIE["a"]) ){
	$co = file_get_contents("u/".$_COOKIE["a"]);
	$co2 = explode("|", $co);
	echo "Hello: " . $co2[0] . "<br>";
	}
	if (!file_exists("u/".$_COOKIE["a"]) ){
	unset($_COOKIE["a"]);
	}

If exists.

If !exists.laugh.png

Wouldn't expect PHP to pertain to standard logics laugh.png

As for the premise of this article - getting something to work though ugly can be a good or bad thing. You need to find a balance where it actually helps laying the foundations for a more solid solution rather than leading to redundant work or bad code. Some programmers with a "fix it later" attitude take it as an excuse for poor code and end up causing more work than necessary because their code must always be rewritten by someone else who could have written it properly immediately in the first place in similar time.

Of course, there are occasions where throwaway/"write only" code is acceptable (usually in leaf parts of a system). Still, not doing your worst pays off due to how much easier bugs become to find.

You can also use this syntax:


<?php if($condition) : ?>
Hello
<?php endif; ?>

Same for all control flow statements. It's easier to keep track of than using braces imo.

This topic is closed to new replies.

Advertisement