Jump to content

[SOLVED] logging in


squiblo

Recommended Posts

this is my code that gets the info from the database but something is wrong, it goes not redirect me to either google or yahoo if my loggin details are correct or not, can anyone help me find the problem please...

 

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$mycompany=$_POST['mycompany']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$mycompany = stripslashes($mycompany);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mycompany = mysql_real_escape_string($mycompany);
//$_SESSION['myusername']=$dbusername;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and company='$mycompany'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword and $mycompany, table row must be 1 row

if($count==1){
// Register $myusername, $mycompany and $mypassword and redirect to file "profile.php"
session_register("myusername");
session_register("mypassword");
session_register("mycompany"); 
header("location:http://www.yahoo.com");
}
else {
header("location: http://www.google.com");
}
}
?>

Link to comment
Share on other sites

Rather than use header() to see what is happing just use echo.

 

if($count==1){
echo 'User is logged in';
}
else {
echo 'User is NOT logged in';
}

 

Also you should not be using session_register for creating session variables. This function is depreciated. You should instead use the the $_SESSION superglobal when defining/using session variables.

 

So instead of

session_register("myusername");
session_register("mypassword");
session_register("mycompany"); 

 

You'd this instead

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;

Link to comment
Share on other sites

so it is this?...

 

if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;
echo 'User is logged in';
}
else {
echo 'User is NOT logged in';
}

Link to comment
Share on other sites

Yes. That is now the correct way for using sessions.

 

What does your script output now? It should display user is logged in when using the correct username/password. For the wrong username/password the message user is NOT logged in will be displayed.

Link to comment
Share on other sites

ok im still getting an error, this is my newest code..

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$mycompany=$_POST['mycompany']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$mycompany = stripslashes($mycompany);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mycompany = mysql_real_escape_string($mycompany);
//$_SESSION['myusername']=$dbusername;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and company='$mycompany'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword and $mycompany, table row must be 1 row

if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;
echo 'User is logged in';
}
else {
echo 'User is NOT logged in';
}
}
?>

 

and this is my html form...

<form name="form1" method="post" action="checklogin.php">
   <input type="text" id="myusername"                            style="font-size:15px;"><br><br>
   <input type="text" id="mycompany"                             style="font-size:15px;"><br><br>
   <input type="password" id="mypassword"                        style="font-size:15px;"><br><br>
   <input type="submit" id="indexsearch" name="log in" value="Log In"  style="height:35px;">
</form>

Link to comment
Share on other sites

i am just getting a blank page, what does it mean if i am getting a blank page and it is failing?

It means there could be an error which is preventing the script from running. You'll get a blank screen is errors are disabled.

 

To enable errors, at the top of your script add the following after the opening <?php tag

error_reporting(E_ALL);
ini_set('display_errors', 'on');

 

If there are any errors they should be displayed. Alternatively you can always check your servers error log

Link to comment
Share on other sites

i am still getting a blank page with this...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$mycompany=$_POST['mycompany']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$mycompany = stripslashes($mycompany);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mycompany = mysql_real_escape_string($mycompany);
//$_SESSION['myusername']=$dbusername;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and company='$mycompany'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword and $mycompany, table row must be 1 row

if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;
echo 'User is logged in';
}
else {
echo 'User is NOT logged in';
}
}
?>

Link to comment
Share on other sites

still getting a black screen...  :shrug: :shrug:

 

<form name="form1" method="POST" action="checklogin.php">
   <input type="text" id="myusername"      name="myusername"    style="font-size:15px;"><br><br>
   <input type="text" id="mycompany"       name="mycompany"     style="font-size:15px;"><br><br>
   <input type="password" id="mypassword"  name="mypassword"    style="font-size:15px;"><br><br>
   <input type="submit" id="Login" name="Login" value="Log In"  style="height:35px;">
</form>

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$mycompany=$_POST['mycompany']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$mycompany = stripslashes($mycompany);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mycompany = mysql_real_escape_string($mycompany);
//$_SESSION['myusername']=$dbusername;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and company='$mycompany'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword and $mycompany, table row must be 1 row

if($count==1){
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;
header("location:about.php");
}
else {
echo 'User is NOT logged in';
}
}
?>

Link to comment
Share on other sites

There was no

session_start();

,

 

Here i cleaned it up aswell.

 

<?php session_start();

error_reporting(E_ALL);

ini_set('display_errors', 'on');

$host="localhost"; 
$username="";
$password=""; 
$db_name=""; 
$tbl_name="members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

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

$myusername = mysql_real_escape_string(stripslashes($_POST['myusername'])); 

$mypassword = mysql_real_escape_string(stripslashes($_POST['mypassword']));

$mycompany = mysql_real_escape_string(stripslashes($_POST['mycompany'])); 


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and company='$mycompany'";

$result = mysql_query($sql);

if(mysql_num_rows($result) == '1'){

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['mycompany'] = $mycompany;

echo 'User is logged in';

} else {

echo 'User is NOT logged in';

  }
}
?>

 

James.

 

 

Link to comment
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.