/    Sign up×
Community /Pin to ProfileBookmark

Forum Building Question

Ive begun to build a simple forum, nothing huge but I do have a question. When Im doing the login im going to use a session. to tell who the user is would I just set a session variable to username like this for my login page after they type in username and pass

[code=php]
$query=”SELECT * FROM `reforum_users` WHERE username=’$_POST[‘username’] LIMIT 1;
$result=mysql_query($query);
$row=mysql_fetch_assoc($result);
if($row[‘password’]==$_POST[‘passsword’])
{
$_SESSION[‘loggedin’]=”1″;
$_SESSION[‘username’]=$row[‘username’];
echo”Thank you for logging in”;
}
else
{
$_SESSION[‘loggedin’]=”0″;
$_SESSION[‘username’]=NULL;
echo”Incorrect Password for corresponding username please try again”;
}
[/code]

With this could I reference username throughout the rest of the forum.

Heres my progess so far to anyone interested [url]http://www.complexfellow.com/forum/[/url]

to post a comment
PHP

35 Comments(s)

Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — hmm im getting an error with this

[code=php]
<?include("functions.php");?>
<div class="container">
<?
if($action=="")
{
?>
<div>
<form action="/forum/login.php?action=login" method="post">
<div>Username<input type="text" name="username" /></div>
<div>Password<input type="password" name="pass" /></div>
<div><input type="submit" value="Login!" /></div>
</form>
</div>
</div>

<?
}
if($action=="login")
{
$username=$_POST['username'];
$query="SELECT * FROM reforum_users WHERE username=$username";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
if($row['password']==$_POST['passsword'])
{
$_SESSION['loggedin']="1";
$_SESSION['username']=$row['username'];
echo"Thank you for logging in".$_SESSION['username']."<br>";

}
else
{
$_SESSION['loggedin']="0";
$_SESSION['username']=NULL;
echo"Incorrect Password for corresponding username please try again";
}
}
?>
</div>
<?
include("footer.php");
?>

[/code]


The error is Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/refresh/public_html/forum/login.php on line 23

I also tried making line 23 this
[code=php]
while($row=mysql_fetch_array($result))
[/code]


Im connecting in the include and it works on my other pages
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — ok I got it working but the session variables dont seem to be accessible outside of the while loop?

^^thats hammered out,now to try and pass it the sessions to other pages.

Im pulling my hairs out as to why it will not perform the else statement

[code=php]
<?require("functions.php");?>
<div class="container">
<?
if($action=="")
{
?>
<div>
<form action="/forum/login.php?action=login" method="post">
<div>Username<input type="text" name="username" /></div>
<div>Password<input type="password" name="pass" /></div>
<div><input type="submit" value="Login!" /></div>
</form>
</div>
</div>

<?
}
if($action=="login")
{
$username=$_POST['username'];
$query="SELECT * FROM reforum_users WHERE username='$username'";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
if($row['password']==$_POST['pass'])
{
$_SESSION['loggedin']="1";
$_SESSION['username']=$row['username'];
echo"Thank you for logging in&nbsp;".$_SESSION['username']."<br /><a href='/forum' title='index'>Return to Index</a>";
}
}
}
else
{
echo"Incorrect Username Or poassword";
}



?>
</div>
<?
include("footer.php");
?>
[/code]


If the username and password is correct it works but if its not then it doesnt echo anything.
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — ok all that stuff=hammered out. Now is tehre anyway to show the thread with the newest reply at the top of a board?
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Use SORT ASC or SORT DESC for ascending or descending order.[/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — yeah Jona I know that. What I want to to do is put the thread that has the most recent reply it in at the top.

Like in these forums if this was the third thread on the board and I posted in it it would be the first thread on the forum
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Hmm, you've asked a question I've never answered before... lol. You could try adding a "entrynum" field, and sort the posts by that -- each time someone replies to the post, add one to it... Actually that would be sorting by the amount of replies, basically. lol. Hmm...[/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — I got it working.

I added a lastpost field that is a timestamp and it sets the time to NOW when the thread is created then when theres a new post I perform an update query that bumpts it to the top.
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Ah, good call! ? [/font]
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Hey, could I suggest a logout button? I just registered, but I can't find a logout button. Just use session_destroy() if logged in, and if the user is not logged in, say something like, "You cannot log out because you are not logged in!"[/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — yeah could idea,but ill only display it if the user is logged in.
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Even so, you don't want errors to appear -- someone could guess the URL. So it's usually best to have some sort of validation anyway.[/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — im having some problems. It is inserting the time() function as 00000000000000 everytime so its not putting the newest at the top since there are a bunch with 00000000000000. Any idea why and is there another function I could use.
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]Odd... What code are you using? I don't think there is an alternative to the UNIX timestamps... Maybe with date() but that's kind of useless, since time() is always the best thing to go with, from what I understand.[/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — Ok now when I have a new thread and insert the time() function it just comes up as 0 but when i perform the update query during replies it puts the actual time() ? so new threads are going to the bottom

GOT IT WORKING NOW
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — added logout, thanks for the idea Jona I didnt even know about session_destroy()
Copy linkTweet thisAlerts:
@JonaMay 15.2004 — [font=arial]No problem, looks pretty good so far! Good job! ? [/font]
Copy linkTweet thisAlerts:
@ConorauthorMay 15.2004 — thanks my next big thing to think about is pagination, I have no idea how this works so I guess ill start looking it up.
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — uggh Im having massive session problems. I added profiles and when you click someones profile it logs you in as them? any idea why

profile code

[code=php]
<?
include("functions.php");
echo"<div class='container'><h3>$user 's Profile</h3><table width='100%' cellspacing='0' border='1'>";
$user=$_GET['user'];
$query="SELECT * FROM reforum_users WHERE username='$user'";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result))
{
$id=$row['ID'];
$username = $row['user'];
$name = $row['name'];
$email = $row['email'];
$postcount = $row['postcount'];
$reg= $row['registered'];

echo "<tr class='odd'><td class='left'>Username</td><td class='post'>$username</td></tr>"
."<tr class='even'><td class='left'>Name</td><td class='post'>$name</td></tr>"
."<tr class='odd'><td class='left'>Email</td><td class='post'>$email</td></tr>"
."<tr class='even'><td class='left'>Post Count</td><td class='post'>$postcount</td></tr>"
."<tr class='odd'><td class='left'>Reg Date</td><td class='post'>$reg</td></tr>";

}
echo"</table></div>";
include("footer.php");
?>


[/code]


login code

[code=php]

<?
$pageTitle="ReForum::Log In";
require("functions.php");?>
<div class="container">
<?
if($action=="")
{
?>
<div>
<form action="/forum/login.php?action=login" method="post">
<div>Username<input type="text" name="username" /></div>
<div>Password<input type="password" name="pass" /></div>
<div><input type="submit" value="Login!" /></div>
</form>
</div>
</div>

<?
}
if($action=="login")
{
$passw=$_POST['pass'];
$passw=md5($passw);
$username=$_POST['username'];
$query="SELECT * FROM reforum_users WHERE username='$username'";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
if($row['password']==$passw)
{
$_SESSION['loggedin']="1";
$_SESSION['username']=$row['username'];
echo"Thank you for logging in&nbsp;".$_SESSION['username']."<br /><a href='/forum' title='index'>Return to Index</a>";
}
else
{
echo"Incorrect Username Or password";
}
}
}
?>
</div>
<?
include("footer.php");
?>

[/code]
Copy linkTweet thisAlerts:
@JonaMay 16.2004 — [font=arial]Shouldn't it be...[/font]

[code=php]
$user=$_GET['user'];
echo "<div class='container'><h3>$user's Profile</h3><table width='100%' cellspacing='0' border='1'>";
[/code]
Copy linkTweet thisAlerts:
@The_CheatMay 16.2004 — dont you think you are reinventing the wheel?
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — yeah I fixed that now but It still doesnt change that when you go into a users profile it logs you in as them. I put profies back in so you can see.

Cheat I dont understand your post.
Copy linkTweet thisAlerts:
@The_CheatMay 16.2004 — well there are tons of free full-featured forums already out there. So dont you think making your own is kind of a waste of time..? Unless its for a learning experience i guess...

thats all i meant
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — Ok now I understand what you mean, but yeah its just for a learning experiance, I already admin over at an already downloading forum but I get alot more satisfaction doing things myself.
Copy linkTweet thisAlerts:
@The_CheatMay 16.2004 — ah, ok ? thats cool then
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — yeah , but this session thing is driving me crazy
Copy linkTweet thisAlerts:
@NevermoreMay 16.2004 — You are using the variable 'username' to hold the logged in user and the profile owner's usernames. Change one of them to a different variable name.
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — yeah but why would that affect if in login.php and profile.php arent connected in anyway

I dont know why that worked but it did thanks
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — ok When I echo out the posts I use a while loop to echo out everything from the table reforum_posts, how would I echo out the corresponding post count for the user from reforum_users?
Copy linkTweet thisAlerts:
@NevermoreMay 16.2004 — [i]Originally posted by RefreshF5 [/i]

[B]yeah but why would that affect if in login.php and profile.php arent connected in anyway



I dont know why that worked but it did thanks [/B]
[/QUOTE]


Think about it; you are using the variable $username to hold the logged in user. If you reset that variable to another value, then the code will use that new value as the logged in user; you were setting $username to the user you were looking up, so you got logged in as them.
[I]Originally posted by RefreshF5[/I]

[b]ok When I echo out the posts I use a while loop to echo out everything from the table reforum_posts, how would I echo out the corresponding post count for the user from reforum_users?[/b][/QUOTE]


What's the problem? Why can't you just use a standard MySQL query to get the post count? As long as you make sure you use different variable names (not query and result again) then there shouldn't be a problem with running multiple queries.
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — My question is how to echo out during the other while loop.

Would I put the query inside the other while loop so i could select it where the username ont the page is equal to the username in the database and then run another while to echo that out as well
Copy linkTweet thisAlerts:
@Daniel_TMay 16.2004 — [url="http://www.complexfellow.com/forum/thread.php?ID=61&boardid=3&board="]I hax0red it[/url]. You may want to fix that.

-Dan
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — pretty good idea of how ill fix it
Copy linkTweet thisAlerts:
@ConorauthorMay 16.2004 — I believe I have fixed it, can you do whatever you did before
Copy linkTweet thisAlerts:
@ConorauthorMay 17.2004 — so can i run one while in another like I mentioned in the first post on this page.
Copy linkTweet thisAlerts:
@JonaMay 17.2004 — [i]Originally posted by RefreshF5 [/i]

[B]so can i run one while in another like I mentioned in the first post on this page. [/B][/QUOTE]


[font=arial]You should be able to, providing your variable names are all different; though, in a lot of cases, you can use an if statement and save processing time - but not if you're doing multiple queries.[/font]
×

Success!

Help @Conor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...