Jump to content

Beginner php user login system


dennell

Recommended Posts

Hi php freaks,

 

I am looking for some help to customise my code so that a user can update their user login details.

 

This is my poor attempt at creating a session so that a user can login and get a customised message.

 

I have created a very basic login system which is just for a small project of mine. I have created a mysql database and a table to which i can add users.

 

The first thing that I have created is my register new user code which looks like this:

 

register_code.php

 

<?php
 $con = mysql_connect("localhost","root","root");
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }
 
mysql_select_db("testdb", $con);
 
$sql="INSERT INTO customer (firstname, surname, email, username, password)
 VALUES
 ('$_POST[firstname]','$_POST[surname]','$_POST[email]','$_POST[username]','$_POST[password]')";
 
if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
 header('Location: register_success.html');
 
mysql_close($con);
 ?> 

 

The code above with insert a new user into my customer table and register the new user.

 

The next code file is my checklogin.php file which when a user logs in the details are checked and then the user either successfully logs in or doesn't depending on whether their details match the information in my database:

 

checklogin.php

 

<?php

ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="testdb"; // Database name 
$tbl_name="customer"; // 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");

// Define $myusername and $mypassword 
$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 username='$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['myusername'];
$_SESSION['mypassword']; 
header("location:membersonly.php");
}
else {
echo "Wrong Username or Password";
echo '</br><a href="main_login.php">Try Again</a>';
}
ob_end_flush();
?>

 

When the checklogin.php is successful then the user is redirected to the membersonly.php page where I would like to just simply display a welcome message like "welcome bob to your members page"

 

membersonly.php

 

<?php
session_start();
?>

<html>
<body>
Welcome <?=$_SESSION['myusername'];?> Login Successful
</body>
</html>

 

if anyone could help by helping me to adapt my code you will be helping an absolute beginner understand sessions.

 

Thanks

 

Ryan

Link to comment
Share on other sites

You will need to assign values to your sessions in order to register them. Please see below:

 

 

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $result['username'];
$_SESSION['mypassword'] = $result['password'];
 

I am assuming that the fields in your database are named username and password.

Edited by parkerj
Link to comment
Share on other sites

Ok i have assigned the values to my sessions. 

 

However when i run the code and it passes to the membersonly.php it is still not working. As in displaying the "username".

 

I am also looking to have the code changed so that a user can update their details once they are logged in?? any ideas?

 

thanks

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.