• (3 Pages)
  • +
  • 1
  • 2
  • 3

Basic Login Script with PHP A rudimentary login script tutorial aimed at those looking to learn ho Rate Topic: ***-- 2 Votes

#31 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Posted 13 March 2010 - 11:49 AM

thank you. easy to follow.
a question: when a user is logged in, how do i print their username on login success page.

This is what I have on my Check Login Page:
$username = $_POST['user'];
$password = $_POST['pass'];
$_SESSION['$username'];


This is what i have on my Login Success Page:
<?php echo $_SESSION['$username']; ?>

Was This Post Helpful? 0
  • +
  • -

#32 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Posted 14 March 2010 - 07:35 PM

View Postsfw, on 13 March 2010 - 01:49 PM, said:

thank you. easy to follow.
a question: when a user is logged in, how do i print their username on login success page.

This is what I have on my Check Login Page:
$username = $_POST['user'];
$password = $_POST['pass'];
$_SESSION['$username'];


This is what i have on my Login Success Page:
<?php echo $_SESSION['$username']; ?>


session_start(); // At the top of the page
$_SESSION['username'] = $_POST['user'];



Then on a different page you can use $_SESSION['username']. Just make sure you have session_start() at the top of each page you want to have session variables in.
Was This Post Helpful? 0
  • +
  • -

#33 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Posted 17 March 2010 - 07:59 AM

very nice. thank you for reply.
Was This Post Helpful? 0
  • +
  • -

#34 Guest_MikeCobb78*


Reputation:

Posted 26 April 2010 - 11:45 PM

I am having problems with my login page. I have a condition in here that should send the user to a failed login screen but it sends everyone to it when they click on the login page link. It is processing it right away. I have like you can see below the PHP code first and I think I need to split it up to make it work but the header commands won work if they are below in the HTML code. So I was wondering if anyone can please help me out and look this thing over form me real quick to see what it is I did wrong. More than likely it is just a simple fix nothing to big but I have been looking at this thing for a while and everything I try doesn't work.


<?php
session_start(); //starts a new session


$username = "******";
$password = "******";
$host = "localhost";

$database = "******";

$conn = mysql_connect($host, $username, $password);

$database = mysql_select_db($database, $conn) or die ("Could not select database: " . mysql_error() );

//POST Variables
$username = $_POST['user'];
$password = $_POST['pass'];
$hash = sha1($password);
$_SESSION['username'] = $username;
$count = 0;

$sql = "select * from account where username = '$username' and password = '$hash'";
$result = mysql_query($sql) or die ( mysql_error() );




$count = mysql_num_rows($result);

if ($count == 1) {
         $_SESSION['loggedIn'] = "true";
         header("Location: final_project.php"); // This is wherever you want to redirect the user to
} else {
	$_SESSION['loggedIn'] = "false";
	header("Location: loginFail.php");
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <link rel="stylesheet" type="text/css" href="blogotest.css" media="screen" />
<title>Final Project Account Creation</title>


</head>

<body link="#FFFFFF" >
<div id="Header"></div>
<div id="NavRuler"></div>
<div id="NavRuler2"></div>
<div id="Nav">
<!-- The button for navigation are listed below --->

<a href="final_project.php">
<img border="0" id='button880' width="60" height="25" 
src="buttonHome.png"
onmouseover = 'document.getElementById("button880").src = "buttonHomePressed.png"'
onmouseout =  'document.getElementById("button880").src = "buttonHome.png"'
></a>
&nbsp;
<a href="Stats.php">
<img border="0" id='button881' width="60" height="25" 
src="buttonStats.png"
onmouseover = 'document.getElementById("button881").src = "buttonStatsPressed.png"'
onmouseout =  'document.getElementById("button881").src = "buttonStats.png"'
></a>
&nbsp;
<a href="Blog.php">
<img border="0" id='button882' width="60" height="25" 
src="buttonBlog.png"
onmouseover = 'document.getElementById("button882").src = "buttonBlogPressed.png"'
onmouseout =  'document.getElementById("button882").src = "buttonBlog.png"'
></a>

</div>
<div id="wrap">
<div id="back">
<div id="Content">
<font color="#CCCCCC">

<h2>Login To Red Sox Nation</h2>
<br />
<br />
<form action="login.php" method="post">
         <table>
                  <tr>
                           <td>Username: </td>
                           <td><input type="text" name="user"></td>
                  </tr>
                  
                  <tr>
                           <td>Password: </td>
                           <td><input type="password" name="pass"></td>
                  </tr>
                 
                  <tr>
                           <td><input type="reset" value="Reset" name="Reset" />&nbsp;
                           <input type="submit" name="Submit" value="Submit" /></td>
                           <td>&nbsp;</td>
                  </tr>
         </table>
</form>
<br /><br />
<br />

</font></div></div></div>
<div id="Footer"></div>
</body>
</html>




Was This Post Helpful? 0

#35 deplicator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 11

Posted 26 October 2011 - 01:02 PM

I just signed up for dreamincode.net to say thanks for the tutorial. It was apparently just what I needed to kick start the brain into PHP and mySQL. Since following your tutorial I've gone on to figure out how to not store my password in plain text and today I can say I finally get what SQL injection is. The first few comments really bash it, but it is as advertised--for those of us still learning. It's perfect and I thank you for helping me take a big step towards wrapping my head around coding.
Was This Post Helpful? 0
  • +
  • -

#36 srilatha5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-June 13

Posted 21 June 2013 - 04:42 AM

i need to display the username when user logged in.

here is my checklogin.php

<?php
session_start();
$_SESSION['myusername'] = $_POST['Email'];
//print_r($_SESSION['url']);


ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="apple genomics"; // Database name
$tbl_name="users"; // Table name

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

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);



$sql="SELECT * FROM $tbl_name WHERE Email='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){


// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['Email'] = '$myusername';
$_SESSION['Password'] = '$mypassword';
//session_register("myusername");
//session_register("mypassword");
//header("Location: " . $_SESSION['url']);
//$_SESSION['Email']= $_POST["myusername"];
header("location:login_success.php");

}
else {
echo "wrong Name or Password";
}
ob_end_flush();
?>




here is my login_sucess.php

<?php
session_start();
$_SESSION['myusername'] = $_POST['$myusername'];

$_SESSION['url'] = $_SERVER['REQUEST_URI'];

//if(!session_is_registered(myusername)){
if(!isset($_SESSION['Email'])){
echo "You are Welcome ". $_SESSION['Email'];
header("location:login_success.php");
}

?>

<html>
<body>
<h3>welcome to your home page!</h3>

<nav><img src="images/funnyapp.jpg" alt="funnyapp.jpg" width="60" height="60"></img></nav>

       
<p>
<a href="logout.php">Logout</a></p>
</body>
</html>



i got error Undefined index: $myusername in C:\wamp\www\login_success.php on line 49
Was This Post Helpful? 0
  • +
  • -

#37 rich_hemmings  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 05-November 08

Posted 24 April 2014 - 05:03 AM

Hey guys hope your all well!

I just need some help on the login script I stumbled across this script and I'm getting the following error message now when I try and get users to log in


Notice: Undefined index: user in C:\xampp\htdocs\login.php on line 7
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' where username = '' and password = 'password'' at line 1

This is the Login.php script

<?php
// checkLogin.php

session_start(); // Start a new session
require('config.php'); // Holds all of our database connection information
// Get the data passed from the form
$username = $_POST['user'];
$password = $_POST['password'];

// Do some basic sanitizing
$username = stripslashes($username);
$password = stripslashes($password);

$sqli = "select * from users where username = '$username' and password = '$password'";  

$result = mysql_query($sqli) or die ( mysqli_error() );
$count = 0;
while ($line = mysqli_fetch_assoc($result)) {

     $count++;
	 }
if ($count == 1) {

     $_SESSION['loggedIn'] = "true";

     header("Location: loginSuccess.php"); // This is wherever you want to redirect the user to

} else {

     $_SESSION['loggedIn'] = "false";

     header("Location: loginFailed.php"); // Wherever you want the user to go when they fail the login

}

 ?> 


This is the config.php
<?php 

$connection = mysqli_connect("localhost","root","","registration");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//mysqli_close($connection);
?> 


Hope someone can help me out with this

Thanks

Rich
Was This Post Helpful? 0
  • +
  • -

#38 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Posted 24 April 2014 - 07:57 AM

Weird to see a response to something I wrote 6 years ago. My how times have changed.

First, please replace stripslashes with mysql_real_escape_string. I was young and ignorant.

Check to make sure don't have any extra single quotes or to make sure you don't have any extra double quotes. That error is indicating that mysql can't execute the query, so there's something wrong with it. Can you print out the entire query you're generating and repost?

Should probably go back and rewrite this tutorial.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3