Page 1 of 1

Code will not allow html to show.

Posted: Mon Jun 06, 2011 4:00 pm
by DonVito186
I followed a tutorial on youtube from betterphp about using MySQL and PHP to create a login and register system for a web site. I followed as the tutorial showed, up to part 3. The problem is when i have the php/sql code in the file, it wont let the html code show up on the screen. So all i get is a blank page. But when i remove the php code the html code shows up and i see what i want to. Below is my script used:
<?php 

include('core/init.inc.php'); 

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){ 
if (empty($_POST['username'])){ 
$error[] = 'The username cannot be empty.'; 
} 

if (empty($_POST['password']) || empty($_POST['repeat_password'])){ 
$errors[] = 'The password cannot be empty.'; 
} 

if ($_POST['password'] !== $_POST['repeat_password']){ 
$errors[] = 'Password verification failed.': 
} 

if(user_exists($_POST['username'])){ 
$errors[] = 'The username you entered is already taken.'; 
} 

if (empty($errors)){ 
add_user($_POST['username'], $_POST['password']); 

$_SESSION['username'] = htmlentities($_POST['username'])); 

header('Location: protected.php'); 
die(); 
} 
} 

?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>HELLO</title> 
</head> 
<body><p> 
<?php 



?> 
</p> 
<form action="" method="post"><p> 
<label for="username">Username:</label> 
<input type="text" name="username" id="username" /></p> 
<p> 
<label for="password">Password:</label> 
<input type="password" name="password" id="password" /></p> 
<p> 
<lablefor="repeat_password">Repeat Password:</label> 
<input type="password" name="repeat_password" id="repeat_password" /></p> 
<p> 
<input type="submit" value="Register" /> 
</p> 
</form> 
</body> 
</html>

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 5:56 pm
by element
In line 15 you have a : instead of ;
and in line 25 is a ) to much...

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 7:06 pm
by jacek
DonVito186 wrote: So all i get is a blank page
That suggests you made a syntax error somewhere and do not have php set up to tell you about it, so it just dies. If you have access to the php.ini file you can set error_reporting = E_ALL and display_errors = On for more info ... http://www.youtube.com/watch?v=4OH0b-x96Zo

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 8:11 pm
by DonVito186
I fixed the miss-spelling that element pointed out. I also edited the ini file to display errors, and it doesnt say anything. I still get a blank screen.

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 8:15 pm
by jacek
Did you restart Apache after editing php.ini ?

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 8:37 pm
by DonVito186
I am not using Apache, but rather IIS7. And yes i did restart it and still nothing.

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 9:00 pm
by jacek
Can you post the full code as it is now ?

Also does your php set up work ? you could try a test file like
<?php

echo 'test';

?>
to check this.

Sorry for the patronising questions, I have to ask :lol:

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 9:06 pm
by DonVito186
My PHP works, i've been working on other pages for my website other than the login/register. Below is the code as is now.
<?php

include('core/init.inc.php');

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
   if (empty($_POST['username'])){
    $error[] = 'The username cannot be empty.';
   }
   
   if (empty($_POST['password']) || empty($_POST['repeat_password'])){
      $errors[] = 'The password cannot be empty.';
   }

   if ($_POST['password'] !== $_POST['repeat_password']){
      $errors[] = 'Password verification failed.';
   }

   if(user_exists($_POST['username'])){
      $errors[] = 'The username you entered is already taken.';
   }
   
   if (empty($errors)){
       add_user($_POST['username'], $_POST['password']);

       $_SESSION['username'] = htmlentities($_POST['username']));
       
       header('Location: protected.php');
       die();
   }
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
      <title>HELLO</title>
</head>
<body><p>
<?php



?>
</p>
<form action="" method="post"><p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" /></p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" /></p>
<p>
<lablefor="repeat_password">Repeat Password:</label>
<input type="password" name="repeat_password" id="repeat_password" /></p>
<p>
<input type="submit" value="Register" />
</p>
</form>
</body>
</html>

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 9:10 pm
by element
Line 25. Syntax error
remove 1 ) :lol:
$_SESSION['username'] = htmlentities($_POST['username']);

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 9:20 pm
by DonVito186
element wrote:Line 25. Syntax error
remove 1 ) :lol:
$_SESSION['username'] = htmlentities($_POST['username']);
That line was in the tutorial and it worked for him, but now its working for me. Thank you so very much!

Re: Code will not allow html to show.

Posted: Mon Jun 06, 2011 10:48 pm
by jacek
DonVito186 wrote:That line was in the tutorial and it worked for him, but now its working for me. Thank you so very much!
I had it with one ) ;)

Also php should have told you about this error :? Something is stopping your errors from being displayed. You will find it very hard to debug / develop with hidden errors.