I would like to start off by saying that this is my first time using the switch and $_GET functions. Most of my programming experince prior to this has been with C++,PERL and basic HTML. I will also note now that my code was provided by a tutorial that I was reading.
The error I'm recieving when the page opens is this "Notice: Undefined index: action in C:\Server\htdocs\register.php on line 60". Now, I'm having a feeling this tutorial is leaving something out about the Switch function. Again, I openly admit I'm very new to this. If anyone has any advice or any resources that more fully explain the Switch and $_GET function, I would be deeply appreciated. I would like to more fully understand what I'm programming than just copying it down.
Also, when I do get this working, is there a way to have my activation code mail_to work with the server being a localhost? for testing purposes I have yet to upload my website to a server and am currently using apache.
<?php
require("config.php");
require("functions.php");
switch($_GET['action']) // LINE 60!!!
{
case "new":
//--------------------------------------
// [New Registration
//--------------------------------------
if(!isset($_POST['register']))
{
echo "
<table width='100%' border='0' cellspacing='0' cellpadding='5' style='padding-top: 20px;'>
<form action='register.php?action=new' method='POST'>
<tr>
<td style='width: 25%; '>Email:</td>
<td style='width: 75%; text-align:left;'><input type='text' name='email' class='register_box' size='40' maxlength='255'></td>
</tr>
<tr>
<td style='width: 20%;'>First Name:</td>
<td style='width: 80%; text-align:left;'><input type='text' name='username' class='register_box' size='20'></td>
</tr>
<tr>
<td style='width: 20%;'>Last Name:</td>
<td style='width: 80%; text-align:left;'><input type='text' name='username' class='register_box' size='20'maxlength='255'></td>
</tr>
<tr>
<td style='width: 20%;'>Password:</td>
<td style='width: 80%; text-align:left;'><input type='password' name='password' class='register_box' size='20' maxlength='255'></td>
</tr>
<td style='width: 20%;'>Confirm Password:</td>
<td style='width: 80%; text-align:left;'><input type='password' name='confirm password' class='register_box' size='20' maxlength='255'></td>
</tr>
<td></td>
<td style='text-align: left;'><input type='submit' name='register' value='Sign Up' class='register_box'size='40' maxlength='255'></td>
</form>
</table>
";
}
elseif(isset($_POST['register']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
// $confirm password = mysql_real_escape_string($_POST['confirm password']);
$activation_code = generateCode(25);
$userq = "SELECT username FROM user_system WHERE username = '$username' LIMIT 1";
$emailq = "SELECT email FROM user_system WHERE email = '$email' LIMIT 1";
//put errors into an array
$errors = array();
if(empty($username))
{
$errors[] = "The username field was blank! <br />";
}
if(mysql_num_rows(mysql_query($userq)) > 0)
{
$errors[] = "The username given is already in use! Please try another one! <br />";
}
if(empty($password))
{
$errors[] = "The password field was blank! <br />";
}
/*if(!$_POST['confirm password']) {
$errors[] = 'Confirm Password field was blank.';
}
if($_POST['password'] != $_POST['confirm password']) {
$errors[] = 'Password field did not match Confirm password field.';
}
*/
if(empty($email))
{
$errors[] = "The email field was blank! <br />";
}
if(mysql_num_rows(mysql_query($emailq)) > 0)
{
$errors[] = "The email given is already in use! Please try another one! <br />";
}
if(count($errors) > 0)
{
foreach($errors as $err)
{
echo $err;
}
}
else
{
$sqlq = "INSERT INTO user_system (username, password, email, is_activated, activation_code)";
$sqlq .= "VALUES ('$username', '".md5($password)."', '$email', '0', '$activation_code')";
mysql_query($sqlq) or die(mysql_error());
echo "Thanks for registering!
You will recieve an email shortly containing your validation code,
and a link to activate your account!";
mail($email, "New Registration, ", "
Thanks for registering on SITE NAME.
Here are your login details:
Username: ".$username."
Password: ".$password."
In order to login and gain full access, you must validate your account.
Click here to validate:
localhost/register.php?action=activate&user=".$username."&code=".$activation_code."
Thanks!
[Webmaster]
");
}
break;
}
case "activate":
//--------------------------------------
// [Activate Account]
//--------------------------------------
if(isset($_GET['user']) && isset($_GET['code']))
{
$username = mysql_real_escape_string($_GET['user']);
if(mysql_num_rows(mysql_query("SELECT id FROM user_system WHERE username = '$username'")) == 0)
{
echo "That username is not in the database!";
}
else
{
$activate_query = "SELECT is_activated FROM user_system WHERE username = '$username'";
$is_already_activated = mysql_fetch_object(mysql_query($activate_query)) or die(mysql_error());
if($is_already_activated->is_activated == 1)
{
echo "This user is already activated!";
}
else
{
$code = mysql_real_escape_string($_GET['code']);
$code_query = "SELECT activation_code FROM user_system WHERE username = '$username' LIMIT 1";
$check_code = mysql_fetch_object(mysql_query($code_query)) or die(mysql_error());
if($code == $check_code->activation_code)
{
$update = "UPDATE user_system SET is_activated = '1' WHERE username = '$username'";
mysql_query($update) or die(mysql_error());
echo "User $username has been activated! Thanks! You may now login!";
}
else
{
echo "The activation code was wrong! Please try again!";
}
}
}
}
else
{
echo "No ID or user given to activate!";
}
break;
}
?>