Help with counter
[nobr]Hi everyone,
I am writing this page for users to register and to login, but the problem i have is knowing who are the most active users.
I am trying to do this with a hit counter, as you can see in my last "if", but is not working as it should be.
Can somebody help with this.
thanks.
<html>
<head>
<title>2 </title>
</head>
<body>
<body bgcolor="#FFFFFF">
<center>
<br>
<%
Cookie[] cookies = request.getCookies();
if (cookies ==null){
cookies =new Cookie[0];
}
Cookie c =new Cookie("users","");
response.addCookie(c);
String username = request.getParameter("username");
String password = request.getParameter("password");
Cookie newuser =null;
if (username!=null && password!=null && username.length()>0){
newuser =new Cookie(username,password);
response.addCookie(newuser);
}
for (int i=0; i<cookies.length; i++){
if (newuser!=null && newuser.getName().equals(cookies[i].getName())){
newuser =null;
out.println("><BR> <H3>" +"That username already exists, please enter another."+"<BR> </H3>");
}
}
if (newuser !=null){
out.println("<H3> Welcome new user: <BR>"+newuser.getName()+":\t"+ newuser.getValue()+"<BR> </H3>");
}
int counter = 0;
if (request.getParameter("registereduser") !=null){
for (int i=0; i<cookies.length; i++){
if (request.getParameter("registereduser").equals( cookies[i].getName() ) ){
out.println("><BR> <H3>" +"Welcome again, "+ cookies[i].getName()+counter++"<BR> </H3>");
}
}
}
%>
</center>
</body>
</html>
[/nobr]
[3496 byte] By [
deroka] at [2007-11-27 6:39:07]

# 2
As you told me, dealing with cookies in this way, is a big security hole, I understand that, but,
I can't store it in a session, neither a database, because, this is an assignment, and I had been told specifically, that I can't use those,
so I have to solve the problem only with cookies.
deroka at 2007-7-12 18:08:09 >

# 3
I'm trying to do it this way:
I have declared an static counter to do the task, and is incrementing everytime the page is visited, but,
I want that depending of the user, it will print a different output, for example:
user "a", visted 3 times,
or user "b" visited 8 times.
I was thinking in a counter for each user, and to save it and restore it every time the user visits.
how can i do that?
<%! static int staticCounter = 1; %>
<%
if (request.getParameter("registereduser") != null){
for (int i=0; i<cookies.length; i++) {
if (request.getParameter("registereduser").equals( cookies[i].getName() ) ){
out.println("><BR> <H3>" + "Welcome again, "+ cookies[i].getName()+staticCounter++"<BR> </H3>");
}
}
}
%>
deroka at 2007-7-12 18:08:09 >

# 5
> You can't be serious? Nobody in their right frame of
> mind would be suggesting an assignment with such
> stupidity? Storing the password in a cookie is like
> leaving your key, in the keyhole of your front door.
> Would you do that as well?
>
> There should be a law against such methods of
> teaching/training. If you are taught wrong, it's most
> likely you will continue developing in a poor, unsafe
> way for your projects in the future.
I understand your point, and we discussed this in class, but that's the way the profesor want's it.
can you help me out with this, please?
deroka at 2007-7-12 18:08:09 >
