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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899132
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899139
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899143
Share on other sites

The name of your submit button should be named as Login

 

ok im still getting an error

Which is what? Post the error you're getting here. Or are you just getting a blank page? If its a blank page then it could mean your script is failing.

 

 

Link to comment
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899155
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899162
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899164
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899168
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
https://forums.phpfreaks.com/topic/170452-solved-logging-in/#findComment-899171
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.