Forum Moderators: coopster
This is my registration php script:
<html>
<head>
<title>untitled</title>
</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">
<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());
//This code runs if the form has been submitted
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] ¦ !$_POST['pass'] ¦ !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM Users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}
// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}
// now we insert it into the database
$insert = "INSERT INTO Users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
?>
<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.<a href=login.php>Click Here to Login</a></p>
<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>
<?php
}
?>
</body>
</html>
This is my login script:
<html>
<head>
<title>untitled</title>
</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">
<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header('Location: home.html') ;
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] ¦ !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($checkusername);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=reg.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
//then redirect them to the members area
header('Location: home.html') ;
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
</body>
</html>
I think I can't redirect to the home page in a php script that has html tags? So how do I this? Please review my script and tell whats wrong? Thanks!
[edited by: dreamcatcher at 9:53 pm (utc) on May 31, 2008]
[edit reason] use example.com. Thanks. [/edit]
and I tried the:
header('Location: http://www.example.com/home.html');
and it produced the usual blank custom screen
So those two didn't work..but thanks. Are there any other suggestions, please?!Does anyone think the registration and login code looks ok? Maybe its another line of the script thats the problem? Idk....
[uk.php.net...]
Try moving your code around so that the PHP processing occurs before you send the HTML to the browser.
dc
<?php
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("Users") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header('Location: http://www.example.com/home.html');
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] ¦ !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$checkusername = mysql_query("SELECT username FROM Users WHERE username = '$username'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($checkusername);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=reg.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
//then redirect them to the members area
header('Location: http://www.example.com/home.html');
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
<html>
<head>
<title>Example.com-Spotting good music & leaving all nonsense behind</title>
</head>
<body><center><object type="application/x-shockwave-flash" allowScriptAccess="never" allowNetworking="internal" height="120" width="585" data="http://example.com/logo.swf">
<param name="allowScriptAccess" value="never" />
<param name="allowNetworking" value="internal" />
<param name="movie" value="http://example.com/logo.swf" />
<param name="play" value="true" />
<param name="loop" value="false" />
<param name="quality" value="high" />
</object>
<body bgcolor="black"><body text="red">
</body>
</html>
I tried and this also didn't work..it also resulted in a blank custom page...
[edited by: dreamcatcher at 9:52 pm (utc) on May 31, 2008]
[edit reason] use example.com. Thanks. [/edit]