Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP/MYSQL User Authentication/display

Status
Not open for further replies.

Rhyan

Programmer
Jul 4, 2000
33
US
I am creating a private community made up of various forms which do varying things. For example:<br><br>A form where a person can go and enter Tournament Wins like<br>1st or 2nd Place, Tournament Name,Tournament Room Name, Date, Time.<br><br>I have a form that allows people to Register for the private Community I'm creating and I have a table in mysql database that houses their user names and passwords.<br><br>But, my question is how can I have them First, log in to the community one time. Then, when they submit different forms with information like tournament wins, have the results go into a table with their username automatically entered so when I write a script to display entries, it shows up with the information and who submitted it. The purpose of this is to see who submits what so that people cannot add information to the database anonymously.<br><br>I think I need to use Cookies and User Authentication system, but i'm not having luck. I've read articles at Bignosebird,webdev,evilwalrus etc with no luck. <br><br>Thanks for any help <br>Rhyan
 
Hi Rhyan,<br><br>Cookies is the easiest way to solve this. You can assign a unique ID to a user when he comes first on your page. But then you have to give this ID back to the server whenever the user requests an other page. That means that all your links must look like &lt;a href=newpage.html?UID=&lt;? echo $UID; ?&gt;&gt;newpage&lt;/a&gt;.<br><br>Here is the code you can use:<br><br>mysql_connect(&quot;$DBHost&quot;,&quot;$DBUser&quot;,&quot;$DBPass&quot;);<br>if ($UID != &quot;&quot;) {<br>$result=mysql(&quot;$DBName&quot;,&quot;SELECT * FROM Users WHERE UserID='$UID'&quot;);<br>$num=mysql_num_rows($result);<br>if ($num == &quot;0&quot;) {<br>$UID=md5(uniqid(rand()));<br>$date=date(&quot;YmdGis&quot;);<br>$utime=time();<br>$sql=&quot;INSERT INTO Users VALUES ('$UID','$utime','$REMOTE_ADDR');&quot;;<br>mysql($DBName,$sql);<br>$sql=&quot;INSERT INTO Usertrack (UserID,IP,Date) VALUES ('$UID','$REMOTE_ADDR','$date');&quot;;<br>mysql($DBName,$sql);<br>Header(&quot;Location:&nbsp;&nbsp;$WebHost/index.php?UID=$UID&quot;);<br>exit;<br>}<br>}<br><br>if ($UID == &quot;&quot;) {<br>$UID=md5(uniqid(rand()));<br>$date=date(&quot;YmdGis&quot;);<br>$utime=time();<br>$sql=&quot;INSERT INTO Users VALUES ('$UID','$utime','$REMOTE_ADDR');&quot;;<br>mysql($DBName,$sql);<br>$sql=&quot;INSERT INTO Usertrack (UserID,IP,Date) VALUES ('$UID','$REMOTE_ADDR','$date');&quot;;<br><br>mysql($DBName,$sql);<br>Header(&quot;Location:&nbsp;&nbsp;$WebHost/index.php?UID=$UID&quot;);<br>exit;<br>}<br><br><br>You must include this into every page!<br><br>If you set a cookie, than all you need to do is to use this line before redirecting the user with the Header function:<br><br>setcookie(&quot;UID&quot;,$UID,time()+7200);<br><br>This sets a cookie with a unique id and 2 hours lifetime.<br><br><br>Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top