Help after successful login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Help after successful login

Post by metjet2 »

I am using a premade chat script that at first just allowed users to put in a username, and then they would enter the chat. So i used a login/registration script but when I have the user go to the chat page, I cant figure out how to make it post THEIR USERNAME in the chatroom.

The code on the CHAT page is

Code: Select all

<?
if (!$_POST["username"]||$_POST["username"]=="Guest") $username="Guest".rand(1000,9999);
else $username=$_POST["username"];
$username=preg_replace("/[^0-9a-zA-Z_]/","-",$username);
$usertype=$_POST["usertype"];
$userroom=$_POST["room"];
$userroom=preg_replace("/[^0-9a-zA-Z\s_]/","-",$userroom);
setcookie("username",urlencode($username),time()+72000);
setcookie("usertype",urlencode($usertype),time()+72000);
if ($userroom) setcookie("userroom",urlencode($userroom),time()+72000);
?>
That is from when the user posted on the original form. But I am not sure how to have it use the username from the database into the chat.

Any ideas?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help after successful login

Post by Celauran »

metjet2 wrote:I am using a premade chat script that at first just allowed users to put in a username, and then they would enter the chat. So i used a login/registration script but when I have the user go to the chat page, I cant figure out how to make it post THEIR USERNAME in the chatroom.

Code: Select all

else $username=$_POST["username"];
This line here is setting their username to whatever username was specified in the form. If you're having them login elsewhere and then redirecting them to the chat page, you'll need to send their username along with them, either through cookies or session data, and then update the above line accordingly.
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: Help after successful login

Post by metjet2 »

That makes sense..I am still new to sending cookies and session data. What would an example look like that would like that i can use for reference?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help after successful login

Post by Celauran »

On the page where you're logging them in, once they've been authenticated, you could use setcookie() to write their username to a cookie. Back on the chat page, you could use something like

Code: Select all

if (!empty($_COOKIE['user']) { $username = $_COOKIE['user']; }
Post Reply