Forum Moderators: coopster

Message Too Old, No Replies

Sessions in php

sessions,logins,counters

         

JuicyScript

10:47 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



Can someone help me to update the count on a particular user's form submission without it affects the rest of the users.
<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['SESS_MEMBER_ID']) ¦¦ (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
header("location: access-denied.php");
exit();
}
?>

$sql="SELECT * FROM members WHERE member_id AND count";
//echo print_r($sql);

$query=mysql_query($sql);
$executor=@mysql_fetch_array($query);

/* After being able to see the sql and compare it to the db information, let's see exactly what's stored in the $result array. */
echo print_r($executor);

// Then let's exit until we know this part of the script works correctly.
//exit();

}
if($executor['count']>=3) {
echo "Error";
exit();
}
//exit();
else {
$sql2="UPDATE members SET count=count+1 WHERE 'member_id' " ;
mysql_query($sql2); }
?>

nanat

1:44 am on Oct 16, 2009 (gmt 0)

10+ Year Member



JuicyScript your script so complicated to understand
try this one its so very simple to authentication Script;

<?php
session_start();
include ("connect.php");

if (isset($_POST["login"]))
{
$user = trim(stripslashes($_POST["Username"]));
$passwd = md5($_POST["Password"]);

$sql = "SELECT userID, Userlvl FROM users WHERE Username= '%s' AND Password = '%s'";
$result = mssql_query( sprintf( $sql, $user, $passwd ) );
$exist = mssql_num_rows($result);

if ($exist > 0)
{
$userID = mssql_result($result,0,"userID");
$userlvl = mssql_result($result,0,"Userlvl");

$_SESSION["UID"] = $userID;
$_SESSION["LEVEL"] = $userlvl;

if ((isset($_SESSION["UID"])) && ($_SESSION["UID"] != ''))
{
if ($_SESSION["LEVEL"] == 1)
{
echo '<script type="text/javascript">
location.href="";
</script>';
}
else
{
echo '<script type="text/javascript">
location.href="";
</script>';
}
}
else
{
echo '<script type="text/javascript">
alert("Unable to set a session! Please try again...");
history.go(-1);
</script>';
}
}
else
{
echo '<script type="text/javascript">
alert("Invalid Password or Username!");
history.go(-1);
</script>';
}
}
else
{
echo '<script type="text/javascript">
alert("Direct Access not allowed!");
history.go(-1);
</script>';
}

?>

JuicyScript

6:13 am on Oct 16, 2009 (gmt 0)

10+ Year Member



Thanks but all i wanna knw is that when a user logs into my website it creates a session.How will i b able to use that session to record the number of times he submit a form into the database.The script above does not allow me to record or update a particular user,but upates all the users in the database.If only i cld get an idea of how to combine sessions with sql statement or somefin.

nanat

6:45 am on Oct 16, 2009 (gmt 0)

10+ Year Member



what do you mean? you want to know how many hits your site has being visited? or a user logs in your log in form?

user logs..


if (!$sql) die();
$f = @fopen('logs.txt', 'a+');
if ($f) {
@fputs($f, date("m.d.Y g:ia")." ".$_SERVER['REMOTE_ADDR']." ".$Username." ".$_SESSION['SESS_MEMBER_ID']." \n");
@fclose($f);
}

JuicyScript

1:07 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Am writing a voting system 4 my school.I've programmed it in such a way that a user can log in only twice.To vote and check the results.The first log in is to vote by clicking and submiting his form.I want to program it in such a way that it counts->1,when u submits a form.And prevents you from getting access to a form again.So far so good.But i can't grab the username and password to do this because am logged in already and using a session.So i want to knw how wld is be possible 4 me to use the username and password or the session to achieve this.

mvaz

1:14 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Could you not disable the account once a user has logged in twice?

JuicyScript

2:38 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Yes but the second time the user logs in he can vote again
I jus wanna disable the form at the 2nd login

idfer

4:02 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



Your SQL statements look a bit strange to me, the WHERE clause doesn't compare the fields with anything, so i suspect they match all rows, you want to do this:

$sql="SELECT * FROM members WHERE member_id = ".$_SESSION['SESS_MEMBER_ID']; // I don't think you really need the " AND count " part

and:

$sql2="UPDATE members SET count=count+1 WHERE member_id = ".$_SESSION['SESS_MEMBER_ID'];

This should work as long as member_id contains numeric values, otherwise you'll need to enclose the right part of the comparisons in single-quotes.

JuicyScript

7:34 am on Oct 17, 2009 (gmt 0)

10+ Year Member



Thanks idfer
It worked