Jump to content

Register?


kirbyweb
 Share

Recommended Posts

Here is my code:

<?phpecho "<h1>Register</h1>";$submit = $_POST['submit'];//form data$firstname = strip_tags($_POST['firstname']);$username = strtolower(strip_tags($_POST['username']));$password = strip_tags($_POST['password']);$repeatpassword = strip_tags($_POST['repeatpassword']);$date = date("Y-m-d");$emailaddress = strip_tags($_POST['emailaddress']);if ($submit){//open my database$connect = mysql_connect('_____', '_____', '_____') or die('Couldn\'t connect!');mysql_select_db('______');$namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");$count = mysql_num_rows($namecheck);if ($count!=0){echo "Username already taken.";}else//check for existenceif ($firstname&&$username&&$password&&$repeatpassword){if ($password==$repeatpassword){//check username and first name lengthif (strlen($username)>25||strlen($firstname)>25){echo "Max characters for first name and username is 25.";}else{//check password lengthif (strlen($password)>25||strlen($password)<6){echo "Password must be within 6-25 characters";}else{//register the user//encrypt passwords$password = md5($password);$repeatpassword = md5($repeatpassword);//generate random for activation process$random = rand("12345678,8765432");$queryreg = mysql_query("INSERT INTO users VALUES ('','$firstname','$username','$password','$emailaddress','$date','$random','0')");$lastid = mysql_insert_id();//send activation email$to = $emailaddress;$subject = "Activate your account";$headers = "From: kirbyfrogfan@gmail.com";$body = "Hello $firstname.\n\nThank you for registering on url.com.In order to log in and play, please click on the link below to activate your account.http://kirbyweb.byethost5.com/activated.php?id=$id&code=$random\n\nThank you.";//function to send emailmail($to, $subject, $body, $headers);die("You have been registered! Please check your email to activate your account. <a href='index.php'>Return to home</a>");}}}elseecho "Your passwords do not match.";}else echo "Please fill in all fields.";}?><html><p><form action='register.php' method='POST'><table><tr><td>Your first name:</td><td><input type='text' name='firstname' value='<?php if ((strlen($firstname)<6)||(strlen($firstname)>25)){echo "";}else {echo $firstname;}?>'></td></tr><tr><td>Choose a username:</td><td><input type='text' name='username' value='<?php if ((strlen($username)<6)||(strlen($username)>25)){echo "";}else {echo $username;}?>'></td></tr><tr><td>Choose a password:</td><td><input type='password' name='password'></td></tr><tr><td>Repeat password:</td><td><input type='password' name='repeatpassword'></td></tr><tr><td>Email address:</td><td><input type='text' name='emailaddress'></td></tr></table><p><input type='submit' name='submit' value='register'></form></html>

It keeps saying could't connect does anyone know why?

Edited by kirbyweb
Link to comment
Share on other sites

It's not about following instructions, it's about writing the code in a way that's easy to understand. When you don't indent the lines it's not easy to read the code. Compare the code you have above with this:

<?phpecho "<h1>Register</h1>";$submit = $_POST['submit'];//form data$firstname = strip_tags($_POST['firstname']);$username = strtolower(strip_tags($_POST['username']));$password = strip_tags($_POST['password']);$repeatpassword = strip_tags($_POST['repeatpassword']);$date = date("Y-m-d");$emailaddress = strip_tags($_POST['emailaddress']);if ($submit){  //open my database  $connect = mysql_connect('_____', '_____', '_____') or die('Couldn\'t connect!');  mysql_select_db('______');    $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");  $count = mysql_num_rows($namecheck);  if ($count!=0)  {	echo "Username already taken.";  }  else	//check for existence	if ($firstname&&$username&&$password&&$repeatpassword)	{	  if ($password==$repeatpassword)	  {		//check username and first name length		if (strlen($username)>25||strlen($firstname)>25)		{		  echo "Max characters for first name and username is 25.";		}		else		{		  //check password length		  if (strlen($password)>25||strlen($password)<6)		  {			echo "Password must be within 6-25 characters";		  }		  else		  {			//register the user						//encrypt passwords			$password = md5($password);			$repeatpassword = md5($repeatpassword);			//generate random for activation process			$random = rand("12345678,8765432");			$queryreg = mysql_query("INSERT INTO users VALUES ('','$firstname','$username','$password','$emailaddress','$date','$random','0')");						$lastid = mysql_insert_id();						//send activation email						$to = $emailaddress;			$subject = "Activate your account";			$headers = "From: kirbyfrogfan@gmail.com";						$body = "Hello $firstname.\n\n						Thank you for registering on url.com.						In order to log in and play, please click on the link below to activate your account.						http://kirbyweb.byethost5.com/activated.php?id=$id&code=$random\n\n						Thank you.						";						//function to send email			mail($to, $subject, $body, $headers);			die("You have been registered! Please check your email to activate your account. <a href='index.php'>Return to home</a>");		  }		}	  }	  else		echo "Your passwords do not match.";	}	else 	  echo "Please fill in all fields.";}?>

It's the exact same code, just much easier to read and understand. You may even be able to spot the else statement without brackets, which works in this context because there's only one (large) statement after it. If you're going to post large pieces of code like this, please indent the code. Like I said, it makes it easier for people to read and help. If you don't want to take the time to make it easier for people to help you, then don't expect people to want to take the time to help you.That being said, I'll also say this: if PHP is reporting that it can't connect to the MySQL database, then either the hostname, username, or password was wrong. If they were all correct then it wouldn't say it can't connect. If it can't reach the server at all it's possible that the MySQL server does not allow remote connections and you're trying to connect remotely.

Link to comment
Share on other sites

OK there is a delay in my webhost so it will continue to say couldn't connect until the delay is fixed, its a online webhost so that is why.I figure out this by changing the couldn't connect to another word, I tested it and it still said couldn't connect.So hopefully the delay will be fixed, sorry for all the trouble.Thanks for the indented code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...