/    Sign up×
Community /Pin to ProfileBookmark

Login form not working? sha1 problem?

I don’t know what exactly the problem is here, but I’m using xampp to run these scripts… it connects to the server, inputs the information just fine into the database with the registration script and everything works fine in these php codes except that after I run this php code to register:

[CODE]
<?php include_once(“scripts/global.php”);
$message = ”;
if(isset($_POST[‘username’])){
$username = $_POST[‘username’];
$fname = $_POST[‘fname’];
$lname = $_POST[‘lname’];
$email = $_POST[’email’];
$pass1 = $_POST[‘pass1’];
$pass2 = $_POST[‘pass2’];
//error handling
if((!$username)||(!$fname)||(!$lname)||(!$email)||(!$pass1)||(!$pass2)){
$message = ‘Please insert all fields in the form below’;
}else{
if($pass1 != $pass2){
$message = ‘Your passwords must match!’;
}else{
//securing the data
$username = preg_replace(“#[^0-9a-z]#i”,””,$username);
$fname = preg_replace(“#[^0-9a-z]#i”,””,$fname);
$lname = preg_replace(“#[^0-9a-z]#i”,””,$lname);
$pass1 = sha1($pass1);
$email = mysql_real_escape_string($email);

//check for duplicates
$user_query = mysql_query(“SELECT username FROM members WHERE username=’$username’ LIMIT 1”) or die(“Could not check username”);
$count_username = mysql_num_rows($user_query);

$email_query = mysql_query(“SELECT email FROM members WHERE email=’$email’ LIMIT 1”) or die(“Could not check email”);
$count_email = mysql_num_rows($email_query);

if($count_username > 0){
$message = ‘Your username is already in use’;
}else if($count_email > 0){
$message = ‘Your email is already in use’;
}else{
//insert members
$ip_address = $_SERVER[‘REMOTE_ADDR’];
$query = mysql_query(“INSERT INTO members (username, firstname, lastname, email, password, ip_address, sign_up_date)VALUES (‘$username’,’$fname’,’$lname’,’$email’,’$pass1′,’$ip_address’,now())”) or die(“Could not insert your information”);
$member_id = mysql_insert_id();
mkdir(‘users/’.$member_id,0755);
$message = ‘You have now been registered!’;
}

}
}
}
?>
[/CODE]

I used this code to enable logins but it keeps coming back with “The information your provided is not correct” when i try to login.. I used the same information logging in as I did to register… not sure what’s wrong

[CODE]
<?php include_once(“scripts/global.php”);
$message = ”;
if(isset($_POST[’email’])){
$email=$_POST[’email’];
$pass=$_POST[‘pass’];
$remember=$_POST[‘remember’];

//error handling
if((!$email)||(!$pass)){
$message = ‘Please enter both email and password fields!’;
}else{
//secure data
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
$query = mysql_query(“SELECT * FROM members WHERE email=’$email’ AND password=’$pass’ LIMIT 1”) or die (‘Could not check.’);
$count_query = mysql_num_rows($query);
if ($count_query == 0){
$message = “The information your provided is not correct”;
}else{
//start the session
$_SESSION[‘pass’] = $pass;
while($row = mysql_fetch_array($query)){
$username = $_row[‘username’];
$id = $_row[‘id’];
}
$_SESSION[‘username’] = $username;
$_SESSION[‘id’] = $id;

if($remember == ‘yes’){
//create cookies
setcookie(‘id_cookie’, $id, time()+60*60*24*100,”/”);
setcookie(‘pass_cookie’, $pass, time()+60*60*24*100,”/”);
}

header(‘Location: home.php’);
}
}

}

?>
[/CODE]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 24.2013 — I don't see a session_start() anywhere?

Also, do you realize that Bill O'Reilly, Billy Bob Thornton, and Chris Evert-Lloyd would (silently) have their names changed to Bill OReilly, BillyBob Thornton, and Chris EvertLloyd when entered into your database? Is that really desirable/needed?
Copy linkTweet thisAlerts:
@thewebiphyerauthorJul 24.2013 — oh, I'm kind of new to php, I did this by watching a tutorial video... and everything works fine except it won't recognize my username and password after i 'register' and i 'login'... what am I missing besides SESSION_START()?
Copy linkTweet thisAlerts:
@NogDogJul 24.2013 — So when you say it's not working, what are the exact symptoms? Are you getting the "The information your provided is not correct" message, or is it something else? For debugging purposes, it's often useful to output the actual query being used, e.g.:
[code=php]
}else{
//secure data
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
// put the query into a variable first:
$sql = "SELECT * FROM members WHERE email='$email' AND password='$pass' LIMIT 1";
$query = mysql_query($sql) or die ('Could not check.');
$count_query = mysql_num_rows($query);
if ($count_query == 0){
$message = "The information your provided is not correct";
// debug only, delete this or change to error_log() later:
die("<pre>$sql</pre>"); // now we can see what we sent to the DB
// end debug
}else{
[/code]
Copy linkTweet thisAlerts:
@rootJul 24.2013 — me thinks NogDog, he is missing two or possibly three things, the mysql_real_escape_string() from his query to make ready the query string and not the $email variable string, instead he is prepping the strings and not the actual query string. Also the curly braces {} from the string that he is using and depending on the server settings, he may need back ticks for the field names.

not this
[code=php]$email = mysql_real_escape_string($email);
$pass = sha1($pass);
// put the query into a variable first:
$sql = "SELECT * FROM members WHERE email='$email' AND password='$pass' LIMIT 1";[/code]


but this
[code=php]$pass = sha1($pass);
// put the query into a variable first:
$sql = mysql_real_escape_string("SELECT * FROM members WHERE email='{$email}' AND password='{$pass}' LIMIT 1");
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 24.2013 — No, you don't want to escape the entire query string, as then you'd be escaping the quotes you want to be actual quotes (and whatever else it escapes).

However, what we [i]really[/i] should be doing here is using MySQL[b]i[/b] (or PDO) and making use of prepared statements with bound parameters.
[code=php]
$pdo = new PDO($dsn, $dbUser, $dpPass);
$sql = $sql = "SELECT * FROM members WHERE email=:email AND password=:password LIMIT 1";
$stmt = $pdo->prepare($sql);
if($stmt == false) {
throw new Exception($pdo->errorInfo());
}
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $pass);
if($stmt->exec() == false) {
throw new Exception($stmt->errorInfo());
}
[/code]

No more messing around with various "escape" functions. ?
Copy linkTweet thisAlerts:
@thewebiphyerauthorJul 24.2013 — I had the edit this reply cause I changed a few things... my code now looks like this:
[CODE]
<?php include_once("scripts/global.php");
$message = '';
if(isset($_POST['email'])){
$email=$_POST['email'];
$pass=$_POST['pass'];
$remember=$_POST['remember'];

//error handling
if((!$email)||(!$pass)){
$message = 'Please enter both email and password fields!';
}else{
//secure data
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
$query = mysql_query("SELECT * FROM members WHERE email='$email' AND password='$pass' LIMIT 1") or die ('Could not check.');
$count_query = mysql_num_rows($query);
if ($count_query == 0){
$message = "The information your provided is not correct";
die("<pre>$query</pre>");
}else{
//start the session
$_SESSION['pass'] = $pass;
while($row = mysql_fetch_array($query)){
$username = $_row['username'];
$id = $_row['id'];
}
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
if($remember == 'yes'){
//create cookies
setcookie('id_cookie', $id, time()+60*60*24*100,"/");
setcookie('pass_cookie', $pass, time()+60*60*24*100,"/");
}
header('Location: home.php');
}
}
}
[/CODE]
that's for login.php

then for home.php it looks like this
[CODE]
<?php include_once("scripts/global.php");
if($logged == 0){
header('Location: index.php');
exit();
}

?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to PlayTip</title>
<link href='css/global.css' rel='stylesheet' type='text/css'/>
</head>

<body>

<div class='container center'>
<h1>Welcome to PlayTip</h1>
This is the logged in display.
<a href='logout.php'>Logout here.</a>
</div>

</body>
</html>
[/CODE]

and global.php looks like this
<i>
</i>&lt;?php
session_start();
include_once("connect.php");

//checking if sessions are set
if(isset($_SESSION['username'])){
$session_username=$_SESSION['username'];
$session_pass=$_SESSION['pass'];
$session_id=$_SESSION['id'];

<i> </i>//check if member exists
<i> </i>$query = mysql_query("SELECT * FROM members WHERE id='$session_id' AND password='$session_pass' LIMIT 1") or die('Could not check member');
<i> </i>$count_count = mysql_num_rows($query);
<i> </i>if($count_count &gt; 0){
<i> </i> //logged in stuff here
<i> </i> $logged = 1;
<i> </i>}else{
<i> </i> header('Location: logout.php');
<i> </i> exit();
<i> </i>}
}else if(isset($_COOKIE['id_cookie'])){
$session_id = $_COOKIE['id_cookie'];
$session_pass = $_COOKIE['id_pass'];

<i> </i>//check if member exists
<i> </i>$query = mysql_query("SELECT * FROM members WHERE id='$session_id' AND password='$session_pass' LIMIT 1") or die('Could not check member');
<i> </i>$count_count = mysql_num_rows($query);
<i> </i>if($count_count &gt; 0){
<i> </i> while($row = mysql_fetch_array($query)){
<i> </i> $session_username = $row['username'];
<i> </i> }
<i> </i> //create sessions
<i> </i> $_SESSION['username'] = $session_username;
<i> </i> $_SESSION['id'] = $session_id;
<i> </i> $_SESSION['pass'] = $session_pass;
<i> </i> //logged in stuff here
<i> </i> $logged = 1;
<i> </i>}else{
<i> </i> header('Location: logout.php');
<i> </i> exit();
<i> </i>}
}else{
//if the user is no logged in
$logged = 0;
}


?&gt;


and it reroutes me to index.php instead of home.php
Copy linkTweet thisAlerts:
@thewebiphyerauthorJul 30.2013 — still no responses? I took another look at the code and thought that maybe when the browser navigates to home.php it is automatically rerouting to index.php because for some weird reason $logged is set to 0, but I don't see why that would be the case. help?
Copy linkTweet thisAlerts:
@rootJul 31.2013 — Why do you need to create and test a variable for if a person is logged in? It would be simpler to just initialize a session on each page, check for a user session existing and have done with that rather than tracking variables that may or may not have been set.

[code=php]<?php
session_start();
include_once("connect.php");

// are we logged in? if not, send user to login...
if( !isset($_SESSION['username'])) header("Location: dologin.php");

// we have a session set, so we must be logged in... so verify that the login is real
$query = mysql_query(sprintf( "SELECT * FROM members WHERE id='%s' AND password='%s' LIMIT 1;--",
mysql_real_escape_string($_SESSION['id']),mysql_real_escape_string($_SESSION['pass']) ));

// the login returns zero rows, its not real, go get them to log in
if( mysql_num_rows($query) == 0 ) header("Location: dologin.php");

//... if all OK, go home!!!
header("Location: home.php");

?>[/code]


The above is an example on cutting away all the clutter, you have too much clutter and IMHO the above example would be better way of tackling the login issue. As for the rest of your scripts, my advice is to take the scripts back to the bare bones.
Copy linkTweet thisAlerts:
@thewebiphyerauthorJul 31.2013 — wow...i guess that would be simpler.. I'll give it a shot and let you know if it works thanks for the suggestion
Copy linkTweet thisAlerts:
@rootJul 31.2013 — I should add that cutting down on code should have one exception and that is the $_FILES, $_POST and $_GET inputs, these are hack points that require these inputs to be sanitized in to safe variables that are then used in place of them.

When sanitize I chose to call my inputs $safe_POST['inputname'], example in your script would be...

[code=php]// use a whitelist to control what inputs are acceptable
$whitelist = array("email","pass","remember");

// make an array to store the sanitized data
$safe_POST = array();

// the sanitizing loop
foreach( $whitelist as $key )
$safe_POST[$key] = santizingfunction( $_POST[$key]);

// you now have an array with sanitized data that you know is safe to use.[/code]


If you want instant variables to use after santizing, you can use the PHP function called extract() that will turn an array in to variables, eg.
[code=php] extract( $safe_POST );[/code] will return variables names $email with the value of the data of $safe_POST['email'] and others named likewise, you should look up how this function is used on php,net

The sanitizingfunction() would be whatever takes your fancy but ideally will remove slashes and HTML code that is designed to break scripts and to hack databases.
Copy linkTweet thisAlerts:
@thewebiphyerauthorAug 02.2013 — I tried what you said an added that script to the beginning of each page, but whenever I try to load any of the pages, it just gets stuck in a redirect loop and doesn't load anything... i think I may just have to find some good php tutorial videos and go back from the very basics and maybe i'll find out why things aren't working
Copy linkTweet thisAlerts:
@rootAug 02.2013 — You need to look at what scripts are "Including" other scripts and the examples I give are for example, they are not a working result. You would need to take the principle and work with your existing program structure.
×

Success!

Help @thewebiphyer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...