Jump to content

Recommended Posts

Hello, I'm having problems with my php register script. Everytime i open it up in my browser i always get  "Notice: Undefined index: username in C:\wamp\www\phpdesigner_tmp1.php on line 5"

 

Here is my script..

 

 <?php

include('connections.php');

$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$user = stripslashes($user);
$pass = stripslashes($pass);
$pass = sha1($pass);
$user = addslashes($user);

if(isset($_POST['submit'])) {

if(!$_POST['username'] || !$_POST['password']) {
die (' Please make sure all of the require fields are entered. ');	
}

$user_check = "SELECT `username` FROM `users` WHERE `username` = '$user' ";
$sql = mysql_query($user_check);
$row = mysql_num_rows($sql);

if ($row > 1) {
die ('Sorry but the username '.$user.' is already in use' );
}
if (strlen($user)< 4) {
die('Please make sure that the username is 4 or more characters');
}
if (strlen($pass)< 4) {
die('Please make sure that the password is 4 or more characters');
}

}


$insert = "INSERT INTO `users` (username, password) VALUES ('$user', '$pass')";
$sql = mysql_query($insert);



?>

<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="password" maxlength="10" />
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register" /></th></tr> </table>
</form> [code]

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/136497-solved-undefined-index-help/
Share on other sites

All this....

 

$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$user = stripslashes($user);
$pass = stripslashes($pass);
$pass = sha1($pass);
$user = addslashes($user);

 

Needs to be within your if statement.

Right i done that, now it's saying

 

Notice: Undefined variable: user in C:\wamp\www\phpdesigner_tmp1.php on line 37

 

Notice: Undefined variable: pass in C:\wamp\www\phpdesigner_tmp1.php on line 37

 

which is

 

 $insert = "INSERT INTO `users` (username, password) VALUES ('$user', '$pass')"; [code]

Yeh i put it before that sql query code and sorry heres my code

 

 

<?php

include('connections.php');



if(isset($_POST['submit'])) {

if(!$_POST['username'] || !$_POST['password']) {
die (' Please make sure all of the require fields are entered. ');	
}

$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$user = stripslashes($user);
$pass = stripslashes($pass);
$pass = sha1($pass);
$user = addslashes($user);	

$user_check = "SELECT `username` FROM `users` WHERE `username` = '$user' ";
$sql = mysql_query($user_check);
$row = mysql_num_rows($sql);

if ($row > 1) {
die ('Sorry but the username '.$user.' is already in use' );
}
if (strlen($user)< 4) {
die('Please make sure that the username is 4 or more characters');
}
if (strlen($pass)< 4) {
die('Please make sure that the password is 4 or more characters');
}

}


$insert = "INSERT INTO `users` (username, password) VALUES ('".$_POST['username']."']', '".$_POST['password']."')";
$sql = mysql_query($insert);



?>

<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="password" maxlength="10" />
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register" /></th></tr> </table>
</form>
[code]

You don't need this;

$user = stripslashes($user);
$pass = stripslashes($pass);

unless get_magic_quotes_gpc() is registered (which it shouldnt be)

 

this line

if ($row > 1) {
   die ('Sorry but the username '.$user.' is already in use' );
}

 

should be

if ($row >= 1) {
   die ('Sorry but the username '.$user.' is already in use' );
}

 

Otherwise you'll be able to have usernames in your database twice

 

if (strlen($user)< 4) {
   die('Please make sure that the username is 4 or more characters');
}
if (strlen($pass)< 4) {
   die('Please make sure that the password is 4 or more characters');
}

I'd do these checks before manipulating strings (and before the sql query) You've already used sha1() on the password so it's going to be a standard length.

 

this code

$insert = "INSERT INTO `users` (username, password) VALUES ('".$_POST['username']."']', '".$_POST['password']."')";
$sql = mysql_query($insert);

 

should be

$insert = "INSERT INTO `users` (username, password) VALUES ('$user', '$pass')";
$sql = mysql_query($insert);

 

You should also move that code up so that it's above the last curly bracket '}'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.