0

I have the following code for signing in and signing up:

<? 

$h='mysqlserver';
$u='user';
$G='password';
$n='batadasename';
session_start();

mysql_connect ($h, $u, $G);

mysql_select_db($n) or die('Cannot select database');

//logging in
if ($_POST['username']) {
$username=$_POST['username'];
$password=$_POST['password'];
if ($password==NULL) {
header("Location: index.php?acntion=empty");
}else{
$query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($query);
if($data['password'] != $password) {
header("Location: index.php?action=empty");
}else{
$query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error());
$row = mysql_fetch_array($query);
$_SESSION["s_username"] = $row['username'];
header("Location: http://www.myweb.com/admin/");
}
}
}
else {
    header("Location: index.php?action=empty");
}

?>

And signing up:

<?


$h='mysqlserver';
$u='user';
$G='password';
$n='batadasename';

mysql_connect ($h, $u, $G);

mysql_select_db($n) or die('Cannot select database');

if (isset($_POST["username"])) {
$username = $_POST["username"];
$password = $_POST["password"];
$checkpassword = $_POST["cpassword"];
$email = $_POST["email"];

if($username==NULL|$password==NULL|$checkpassword==NULL|$email==NULL) {

header("Location: index.php?action=empty");
}else{

if($password!=$checkpassword) {
header("Location: index.php?action=notmatch");
}else{

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'");
$username_exist = mysql_num_rows($checkuser);
$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$email_exist = mysql_num_rows($checkemail);
if ($email_exist>0|$username_exist>0) {
header("Location: index.php?action=aleradyuse");
}else{

$query = "INSERT INTO users (username, password, email) VALUES(‘$username’,'$password’,'$email’)";


$query = 'CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(30) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(40) NOT NULL)';
$result = mysql_query($query);
mysql_query($query) or die(mysql_error());
header("Location: success.php?action=checkmail-validation")

}
}
}
}
?>

Index are where the form is, and this code is from "login.php" and "signup.php" which are on different directories.

I just want to know what do you think and what can I improve the code in. Then, if you know how to restrict files and then also log out, I'll be glad to use your code. Thank you.

  • 1
    Please, before you write **any** more SQL interfacing code, you must read up on [proper SQL escaping](http://bobby-tables.com/php) to avoid severe [SQL injection bugs](http://bobby-tables.com/) like the ones you have here. Also, `mysql_query` should not be used in new applications. It's a deprecated interface that's being removed from future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and is a safer way to compose queries. `$_POST` data never goes directly in a query. – tadman Jan 23 '14 at 19:02
  • Is this a new application? Writing PHP this way is extremely difficult and error-prone compared to using [a development framework](http://codegeekz.com/best-php-frameworks-for-developers/) where user authentication is a standard feature you can use out of the box. – tadman Jan 23 '14 at 19:03
  • thank you, but what do you mean with those SQL injection bugs? –  Jan 23 '14 at 19:04
  • I'm just creating this for accessing to an interface I'm creating. Why do you ask it? –  Jan 23 '14 at 19:06
  • http://en.wikipedia.org/wiki/SQL_injection Also, only setting $_Session['logged'] = true is very easy to bypass because you can edit sessions and set them yourself, so if someone finds out that you probably check if someone is authenticated by looking at the $_Session['logged'], he only has to edit his $_session['logged'] to "true" – vincent kleine Jan 23 '14 at 19:06
  • thank you, so you mean I can use a framework, I looked at laravel, which experts say is really good, but i don't know how it works. Is it like a Library of any programming language? How do I install it and use it? what is there good with it? thanks –  Jan 23 '14 at 19:14
  • A framework is a way of organizing your code better, they usually provide lots of examples. [Laravel](http://laravel.com/) is one of those, and another popular one is [Yii](http://www.yiiframework.com/). Have a look at those and others, check out the documentation, and pick one that fits your style and needs. Most come with a huge library of functionality you can tap into instead of having to write it yourself, code that's tested and production ready. If someone knows your framework, they can work with your code, plus there's lots of documentation written for you. – tadman Jan 23 '14 at 19:17

1 Answers1

0
Community
  • 1
  • 1
Eisa Adil
  • 1,743
  • 11
  • 16