Page 1 of 1

Help after successful login

Posted: Mon Dec 06, 2010 9:29 am
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?

Re: Help after successful login

Posted: Mon Dec 06, 2010 10:55 am
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.

Re: Help after successful login

Posted: Mon Dec 06, 2010 11:14 am
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?

Re: Help after successful login

Posted: Mon Dec 06, 2010 4:20 pm
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']; }