/    Sign up×
Community /Pin to ProfileBookmark

Login Authentication w/ Sessions

It was my goal for a a user name and password to be verified through a database and then log the user in accordingly perpetuating this logged in state with session handling. All that is being shown is a blank page and none of the code appears to be executing. Any thoughts would be appreciated. Here is my code:

[CODE]<?php

//start session and initialize loggedin
session_start();

$_SESSION[‘loggedin’]=0;

if (isset ($_POST[‘username’]) && isset ($_POST[‘password’]))
{
$username=$_POST[‘username’];
$password=$_POST[‘password’];

require (‘dbconnect.php’);

$connect = mysql_connect(“$host”,”$login”,”$dbpassword”);
mysql_select_db(“$dbname”);

if (!$connect)
{
die(‘Could not connect: ‘ . mysql_error());
}
else
{
echo ‘Connected successfully to database!<br>’;
}

//create query
$query=”select * from Login where username=’$username’ and password=’$password'”;

$result=mysql_query($query);

//put userID into an array for use
$fetch=mysql_fetch_assoc($foundID);
$currentID=$fetch[“userID”];

if($result->num_rows > 0)
{
$_SESSION[‘loggedin’]=1;
mysql_close($connect);
header(“location: userfunctions.php?userID=”.$currentID);
}
else
{
echo ‘Your user name and password did not match any records found.’;
echo ‘<br>Click <a href=”login.html”>Here</a> to try again.’;
header(“location: login.html”);
}
}
?>[/CODE]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — Check your site's error logs.
Copy linkTweet thisAlerts:
@blackhole82authorOct 25.2006 — Check your site's error logs.[/QUOTE]
There aren't any.
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — (Untested)

Here would be my way before I test:
[code=php]<?php

session_start();
#check if loged in
if($_SESSION['loggedin'] != 1){
$_SESSION['loggedin']=0;
}
#if not login
if(isset($_POST['username']) && isset($_POST['password']) && $_SESSION['loggedin'] != 1){
$username=$_POST['username'];
$password=$_POST['password'];
#search DB
$query="select * from Login where username='$username' and password='$password'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
#Set Sessions
if($username == $row['username'] && $password == $row['password']){
$_SESSION['loggedin']=1;
$_SESSION['username']=$row['username'];
$_SESSION['password']=$row['password'];
}
}
#Display Message
if($_SESSION['loggedin'] == 0){
echo'You must be logged in';
}elseif($_SESSION['loggedin'] == 1){
echo'Welcome '.$_SESSION['username'].'!';
}


?>[/code]
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — All that is being shown is a blank page and none of the code appears to be executing.[/QUOTE]
This is why I mentioned checking the error logs. It sure seems like an error is occurring and your site prevents sending error messages to the client -- instead placing them in a log file. I've dealt with sites like this before and that is the exact symptom I used to see until all the bugs were worked out of the page. Have you checked with your site host?
Copy linkTweet thisAlerts:
@The_Little_GuyOct 25.2006 — Try doing this to get your error codes.

Some times it works better than what you have.
[code=php]$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die (mysql_error());
mysql_select_db("$dbDatabase", $db) or die (mysql_error());[/code]
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — For debugging purposes, you might also want to add the following lines to the start of your script:
[code=php]
ini_set('display_errors', 1);
error_reporting(E_ALL);
[/code]
Copy linkTweet thisAlerts:
@knowjOct 25.2006 — also echo your variables whilst your testing it works wonders (usually find you put an e instead of an a or something in a name) or missing a (
Copy linkTweet thisAlerts:
@theRamonesOct 26.2006 — i prefer like this .. (untested)

$connect = mysql_connect("$host", "$login", "$dbpassword");

mysql_select_db("$dbname", $connect);

// connection ok

$query = "select count(*) from Login where username='$username' and password='$password'";

$result = mysql_query($query, $connect);

$row = mysql_fetch_row($result);

if($row[0]) {

// validation ok

}

else {

// error handling

}
×

Success!

Help @blackhole82 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,
)...