Jump to content

Need Help With simple PHP login


justinede

Recommended Posts

If anyone has some free time can they make a simple php MYSQL login script for me. All i want is page to login from. A page to register and a protected page using sessions. The protected page is the same for all the registered users and it need to be only for people who have logged in. Thanks for the help.

Link to comment
Share on other sites

ok thanks any way..

 

what do i change to this code to put on my index so if there loged in already it just goes to index1.html

 

<?

session_start();

if(!session_is_registered(myusername)){

header("location:index.html");

}

?>

 

that makes it so if they try to go to index1 it send them to index to login. but if there already logged in what makes them go to index1.html

Link to comment
Share on other sites

You need something like this.

 

<?php
$sql = mysql_query("SELECT users FROM users WHERE username = '$session';
$username = mysql_result($sql, 0);

echo "Hello, $username";

?>

 

Just change "users" "users" and "username", unless your tables and rows are already called that. 

Then, you have to give your session a variable, something like..

 

<?php
session_start();
$session = $_SESSION['username'];
?>

 

And when they login, you have to log their username into a session.

 

If all goes well, it should display their name depending on what is in the database.

Link to comment
Share on other sites

ok. This is my check login..

 

<?php
$host="localhost"; // Host name
$username="ipod"; // Mysql username
$password="admin"; // Mysql password
$db_name="ipod"; // 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");

// 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 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_register("myusername");
session_register("mypassword");
header("location:index1.php");
}
else {
echo "Wrong Username or Password";
}
?>

Link to comment
Share on other sites

Add a session line to create a session variable.

 

<?php
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['username'] = $myusername; //Added line
session_register("myusername");
session_register("mypassword");
header("location:index1.php");
}
?>

 

on all pages where you want the user name to display put this code.

 

<?php
echo $_SESSION['myusername'];
?>

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.