Jump to content

Sessions help


Demont

Recommended Posts

I am still having trouble fully understanding how sessions work.

 

I know the commands to to set a session, start a session, and all of that, I've read everything I could find on Sessions, from PHP 5 for Dummies, to various online tutorials, but I am still failing to understand how they interact with each other.

 

Say if, I have a simple chat system(which I currently do, but the users have to type in the user name to display along side the input textm and both are then shown in the output) how do I go about using sessions to show their username from login, so that they do not have to type in the username?

Link to comment
Share on other sites

When the user logs in you should set a session holding their userID, or whatever unique field you set for them. Then all you have to do when they type something in the chat is store their session in the chat table to figure out who said it. Then when your displaying the information from the database you can set it up to pull who said what with their username next to it because you know who said it.

Link to comment
Share on other sites

[code=php:0]
<title>Lands of Loria</title>
<body bgcolor="#000000" text="#ffffff" link="white" vlink="white" alink="white" style="margin-top: 3px; margin-bottom: 3px; margin-left: 3px; margin-right: 3px;">
<?php
include("header.php");
?>
<?php
//Checks if there is a login cookie
if(isset($_COOKIE['ID_Loria']))

//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_Loria']; 
$pass = $_COOKIE['Key_Loria'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['password']) 
{
}
else
{
header("Location: more.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. 

<a href=index.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check )) 
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else 
{ 

// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']); 
$hour = time() + 3600; 
setcookie(ID_Loria, $_POST['username'], $hour); 
setcookie(Key_Loria, $_POST['pass'], $hour); 

//then redirect them to the members area 
header("Location: more.php"); 
} 
} 
} 
else 
{ 

// if they are not logged in 
?>
<center>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2><h1>Login</h1></td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40"> 
</td></tr> 
<tr><td>Password:</td><td> 
<input type="password" name="pass" maxlength="50"> 
</td></tr> 
<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form>
</center> 
<?php 
} 

?> 

[/code]

 

I know sessions need to before any output, but my question is, with the current way it is set up, with the cookies, what would I do to replace it with sessions?

Link to comment
Share on other sites

I'm not going to do it all for you, but I will tell you what you need to know to do it yourself.

 

You need to call session_start(); at the top of all your scripts

 

To create a session

$_SESSION['whatever'] = "Whatever";

 

To check if a session exists

if(isset($_SESSION['whatever'])){
   //it exists
}

 

Lets look at this line

setcookie(ID_Loria, $_POST['username'], $hour);

 

To make a session instead, you would change that to

$_SESSION['ID_Loria'] = $_POST['username'];

 

See if you can do it from that.

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.