Jump to content

Recommended Posts

Is this code safe, or is it easy to inject my database?

 

<?php

session_start();

$pw=mysql_real_escape_string($_POST['pw']);
$username=mysql_real_escape_string($_POST['username']);



$q="SELECT * from `admin` where username='".$username."' and pw='".$pw."'";
$result= mysql_query($q) or die ("Kunne ikke utføre spørringen : $q." . mysql_error());

if (mysql_num_rows($result) == 0)
{
echo ("<title>Feil</title><body bgcolor='black' text='red' alink='blue' vlink='blue' link='blue'> <center><br><br><br>Feil passord eller brukernavn!<br><br><a href='index.php'>Prøv igjen</a></center> ");
}
else{

$r=mysql_fetch_array($result);
$_SESSION['login_username'] = $r["username"];
$_SESSION['login_forename'] = $r["forename"];
Header("Location: main.php");

}

?>

 

 

 

Feedback is appreciated :)

Regards Morten

Link to comment
https://forums.phpfreaks.com/topic/147084-safe/
Share on other sites

I would do it more like this:

 

session_start();
if(!isset($_SESSION['login_username'])) {
   header("Location: index.php");
   exit;
}

 

You don't really even need that else statement.

 

but, what I do to check for that is, I create a session variable and set it to true when the user logs in, then I check like this:

session_start();
if(!$_SESSION['logged']) {
   header("Location: index.php");
   exit;
}

Link to comment
https://forums.phpfreaks.com/topic/147084-safe/#findComment-772164
Share on other sites

Can you give me an example on how to set the session variable to true?

 

I would do it more like this:

 

session_start();
if(!isset($_SESSION['login_username'])) {
   header("Location: index.php");
   exit;
}

 

You don't really even need that else statement.

 

but, what I do to check for that is, I create a session variable and set it to true when the user logs in, then I check like this:

session_start();
if(!$_SESSION['logged']) {
   header("Location: index.php");
   exit;
}

Link to comment
https://forums.phpfreaks.com/topic/147084-safe/#findComment-772167
Share on other sites

On the page you set your sessions, do this for the last one:

 

session_start();
$_SESSION['id'] = $row['id'];
$_SESSION['first'] = $row['first'];
$_SESSION['last'] = $row['last'];
$_SESSION['email'] = $row['email'];
// etc. etc.
$_SESSION['logged'] = TRUE;
header("Location: /userHome.php");
exit;

Link to comment
https://forums.phpfreaks.com/topic/147084-safe/#findComment-772172
Share on other sites

Like this?

 

 

signin.php

<?php

session_start();

$pw=mysql_real_escape_string($_POST['pw']);
$username=mysql_real_escape_string($_POST['username']);

$q="SELECT * from `admin` where username='".$username."' and pw='".$pw."'";
$result= mysql_query($q) or die ("Kunne ikke utføre spørringen : $q." . mysql_error());

if (mysql_num_rows($result) == 0)
{
echo ("<title>Feil</title><body bgcolor='black' text='red' alink='blue' vlink='blue' link='blue'> <center><br><br><br>Feil passord eller brukernavn!<br><br><a href='index.php'>Prøv igjen</a></center> ");
}
else{


$r=mysql_fetch_array($result);

$_SESSION['login_id'] = $r['id'];
$_SESSION['login_username'] = $r["username"];
$_SESSION['login_forename'] = $r["forename"];
$_SESSION['login_surname'] = $r["surname"];
$_SESSION['logged'] = TRUE;
header ("Location: main.php");

}


?>

 

validation.php

<?php
session_start();
if(!$_SESSION['logged']) {
   header("Location: index.php");
   exit;
}

?>

 

Thank you for your help so far.

 

Regards Morten

 

Link to comment
https://forums.phpfreaks.com/topic/147084-safe/#findComment-772175
Share on other sites

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.