Jump to content

[SOLVED] session help


ngreenwood6

Recommended Posts

I am trying to register my sessions with all the user data. Right now it is only registering their username and password because that is all that is required to login. However, in the database their firstname, lastname, and email are all stored. I can echo anywhere in my pages this:

 

echo $_SESSION['myusername'];

 

This will show their username. I want to be able to show their first and lastname but it is not registered because it is not part of the login. My check login page is:

 

<?php

$host="localhost"; // Host name 

$username="root"; // Mysql username 

$password=""; // Mysql password 

$db_name="login"; // 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']; 

// encrypt password 
$encrypted_mypassword=md5($mypassword);

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

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_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:logged_in.php");

}

else {

header("Location:error.php");

}

?>




 

Can anyone help? Any help is appreciated, if you need any additional info please ask.

Link to comment
https://forums.phpfreaks.com/topic/118089-solved-session-help/
Share on other sites

Change this:

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

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


header("Location:logged_in.php");

}

 

To this:

 

if($count==1){
$row = mysql_fetch_array($result);
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION["username"] = $myusername; 
$_SESSION["first"] = $row['first']; 
$_SESSION["last"] = $row['last'];
// Place other sessions from the DB here
$_SESSION["logged"] = TRUE;  


header("Location:logged_in.php");
exit;
}

Link to comment
https://forums.phpfreaks.com/topic/118089-solved-session-help/#findComment-607542
Share on other sites

This starts the session on each page.

<? 

session_start();

if(!session_is_registered(myusername)){

header("location:login.php");

}

?>

 

and this echo's in the page where I want it displayed

 

<?php
if(session_is_registered('myusername'))
{
echo "You are logged in as ";
echo $_SESSION['myusername'];
echo "!".'<br />';
echo "<a href='logout.php'>Click here</a> to log out!";

}
else
{ 		
?>

 

 

The original code I showed you was the page that allows the user to login.  The login form gets sent to that page.

Link to comment
https://forums.phpfreaks.com/topic/118089-solved-session-help/#findComment-607563
Share on other sites

I do also recommend strengthening your query.

 

To do so, do this:

 

$query= srpintf("SELECT * FROM $tbl_name WHERE username='%s' and password='%s'",mysql_real_escape_string($myusername),mysql_real_escape_string($encrypted_mypassword));
$sql = mysql_query($query);
$result=mysql_query($sql);

Link to comment
https://forums.phpfreaks.com/topic/118089-solved-session-help/#findComment-607613
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.