Click to See Complete Forum and Search --> : PHP problems


neenach2002
02-04-2005, 12:33 AM
I've had this code sitting around for a while, and decided I wanted to complete my member system. There's one glitch...

Parse error: parse error, unexpected $ in /home/angelus/public_html/activate.php on line 13
Why?

Please note...I am stilla bit rusty...I haven't really touched PHP since September...heh
<?
include 'db.php';

?>

<div align="center" class="content">

<? ;

if(isset($_GET['userid']) && isset($_GET['db_password']))

{

$userid = $_GET['userid'];
$password = $_GET['db_password'];



$sql = "SELECT COUNT(*) FROM users WHERE userid = ".$userid." LIMIT 1";
$result = mysql_result(mysql_query($sql), 0);

if(result == 1)
{
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result = mysql_query($sql2);

if($result == 0)
{
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = $userid AND `PASSWORD` = $password";
mysql_query($sql3);
}

else {
?>
That account has already been registered. Please login <a href="login.php">here</a>.
<? ;}
}

else {
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<? ;}
?>

scragar
02-04-2005, 03:46 AM
<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['userid']) && isset($_GET['db_password'])){
$userid = $_GET['userid'];
$password = $_GET['db_password'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_result(mysql_query($sql1), 0);
if(result == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result = mysql_query($sql2);
if($result == 0){
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3);
}else{
?>
That account has already been registered. Please login <a href="login.php">here</a>.
<?
};
}else{
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<?
};
?>
try commenting out one line a time untill you spot the mistake. I've played about with the semicolons a bit to make it more legable and indented it, hope you don't mind but it looks easier to read now.

AdamBrill
02-04-2005, 10:40 AM
I'm not sure why it is telling you that the problem is on line #13, but the problem is probably that you didn't close one of the if statments. I'm not sure exactly where to tell you to put the closing curly bracket in the make it work how you want, but once you fix that, it should work.

neenach2002
02-04-2005, 11:39 AM
EditPad will tell me if one is missing or not! *goes to check in EditPad*

neenach2002
02-04-2005, 11:50 AM
I geta blank page, and the DB is not edited. I don't get any SQL errors, either.

New code:

<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['userid']) && isset($_GET['db_password'])){
$userid = $_GET['userid'];
$password = $_GET['db_password'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_result(mysql_query($sql1), 0);
if(result == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result = mysql_query($sql2);
if($result == 0){
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3);
?>Your account has been activated. You can now login!<?;
}else{
?>
That account has already been registered. Please login <a href="login.php">here</a>.
<?
};
}else{
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<?
};
};
?>

96turnerri
02-04-2005, 12:03 PM
Originally posted by AdamBrill
I'm not sure why it is telling you that the problem is on line #13, but the problem is probably that you didn't close one of the if statments. I'm not sure exactly where to tell you to put the closing curly bracket in the make it work how you want, but once you fix that, it should work.

one feature DW lacks :rolleyes:

neenach2002
02-04-2005, 12:06 PM
Originally posted by 96turnerri
one feature DW lacks :rolleyes:

Agreed. I used to use DreamWeaver...but then the trial ran out..I really liked how I could edit things visually....and how the code was colored...*shrug*...EditPad is working just fine for me...=)

96turnerri
02-04-2005, 12:12 PM
i used to use DW then moved onto EditPad, now back on DW

neenach2002
02-04-2005, 12:18 PM
Fun!

Any one have any ideas why I am now getting a blank page?

neenach2002
02-04-2005, 12:39 PM
Now, I am getting the following script-generated error.

You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.

I am definately clicking on the link that is e-mailed to me!

AdamBrill
02-04-2005, 01:14 PM
Did you check if that userid exists in the database? There is a good possibility that it doesn't exist, in which case you should create it in the database(or allow the other part of the script, assuming there is another part, to create it again) and then try clicking the link again.

neenach2002
02-04-2005, 01:18 PM
I have full access to the database. I am sure that userid exists.

AdamBrill
02-04-2005, 01:22 PM
You could try changing this:

$result = mysql_result(mysql_query($sql1), 0);

To this:

$result = mysql_num_rows(mysql_query($sql1));

neenach2002
02-04-2005, 01:31 PM
I still get the same erorr. I have edited the value for active to '1', so that I am able to login.

And I am getting this error once again..


Warning: Cannot modify header information - headers already sent by (output started at /home/angelus/public_html/header.php:12) in /home/angelus/public_html/checkuser.php on line 44

<?
session_start();
if(!$loggedin == true)
$user_level == Guest;
?>
<html>
<head>
<title>Pure Crisis</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<script language="JavaScript1.2">

function high(which2){
theobject=which2
highlighting=setInterval("highlightit(theobject)",50)
}
function low(which2){
clearInterval(highlighting)
if (which2.style.MozOpacity)
which2.style.MozOpacity=0.3
else if (which2.filters)
which2.filters.alpha.opacity=20
}

function highlightit(cur2){
if (cur2.style.MozOpacity<1)
cur2.style.MozOpacity=parseFloat(cur2.style.MozOpacity)+0.1
else if (cur2.filters&&cur2.filters.alpha.opacity<100)
cur2.filters.alpha.opacity+=10
else if (window.highlighting)
clearInterval(highlighting)
}

</script>

<style type="text/css">

td.off {
background-image: url('images/button_off.gif');
}

td.on {
background-image: url('images/button_on.gif');
}

</style>
<link rel="stylesheet" href="style.css" type="text/css">
</head>


Checkuser.php code

<?
session_start();
include 'db.php';
// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
?>
<div align="center" class="content">
Please enter ALL of the information! <br />
<? ;
include 'login.php';
exit();
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
// Register some session variables!
$loggedin = true;
session_register('userid');
$_SESSION['userid'] = $userid;
session_register('loggedin');
$_SESSION['loggedin'] = $loggedin;
session_register('username');
$_SESSION['username'] = $username;
session_register('email_address');
$_SESSION['email_address'] = $email_address;
session_register('user_level');
$_SESSION['user_level'] = $user_level;

mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");

header("Location: index.php");
}
} else {
include 'style.php'; ?>
<div align="center" class="content">
<?
echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
Please try again!<br />";
include 'login.php';
}
?>
</div>

Zealot
02-05-2005, 02:21 AM
header("Location: index.php"); is what I *Think* is erroring, I'm guessing because db.php already sent header infromation?

neenach2002
02-05-2005, 12:23 PM
I shall try removing it...=)

neenach2002
02-05-2005, 01:26 PM
Yep. It works now.

Activation script still isn't working!


<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['userid']) && isset($_GET['db_password'])){
$userid = $_GET['userid'];
$password = $_GET['db_password'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_num_rows(mysql_query($sql1));
if(result == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if($result2 == 0){
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3);
?>Your account has been activated. You can now login!<?;
}else{
?>
That account has already been registered. Please login <a href="index.php?page=login">here</a>.
<?
};
}
}else{
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<?
};

?>

Zealot
02-05-2005, 04:01 PM
make sure its not a silly error like you didnt copy the right post name or you mispellt it. and is all of that sent through post data? I'd this that the $password = $_GET['db_password'] at least would be in db.php and should already be defined. It seems good, maybe just some small little errors. Im not good with MySQL, so I dont know if the error is there or what. What is the script doing? You dont have any die() functions in there, so it should be displaying somethng, so what is it doing?

neenach2002
02-05-2005, 04:05 PM
It displays the following script-generated error:

You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.

I am using the GET method, and have the variables stored in a link that is e-mailed to the user for activation.

Zealot
02-05-2005, 04:10 PM
ok first off, I think that
};
}
}else{
has one too many }'s. second, make sure it is getting the information with $_GET, just set up where the link goes to with
$userid = $_GET['userid'];
$password = $_GET['db_password'];
echo "userid is $userid";
echo "password is $password";
to see if your error is there

neenach2002
02-05-2005, 04:53 PM
Activation script doesn't error. Now it results in a blank page, with no errors.

Zealot
02-05-2005, 08:24 PM
well Im stumped

neenach2002
02-05-2005, 08:28 PM
Forgot to mention. It doesn't process it, either!:confused: :confused: :confused:

96turnerri
02-05-2005, 10:18 PM
you want to know what the problem is?

look at your logic, its incorrectly formatted

you have if($result == 1) { then inside that if you have if($result == 0) { so it will never pick up $result == 0, because to do so $result has to equal one and the it wont pick it up then obviously because $result == 1 not 0, you need to rewrite you logic

neenach2002
02-05-2005, 10:25 PM
One variable is result, the second one is result2.

96turnerri
02-06-2005, 05:32 AM
like i said your logic is wrong

if(result == 1)
{
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result = mysql_query($sql2);

if($result == 0)
{
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = $userid AND `PASSWORD` = $password";
mysql_query($sql3);
}

should be

if(result == 1)
{
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result = mysql_query($sql2);
} elseif($result == 0)
{
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = $userid AND `PASSWORD` = $password";
mysql_query($sql3);
}

for a start, and mysql_result() will not return 0 or 1 unless the database query returns 1 or 0 as the value in the colomn as an interger, so you need to change

$result = mysql_result(mysql_query($sql), 0);

to

$result = mysql_numrows(mysql_query($sql));

neenach2002
02-06-2005, 12:09 PM
You must have misread my code...


if(isset($_GET['userid']) && isset($_GET['db_password'])){
$userid = $_GET['userid'];
$password = $_GET['db_password'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_num_rows(mysql_query($sql1));
if(result == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if($result2 == 0)


And the value in the column IS an integer. It is either 1 or 0.
If the user has been activated, it is a 1, if it hasn't, it is a 0.
The first if() checks if the user is actually in the database, and if it is, the second if() processes it and activates the user.



1 if(result == 1)
2 {
3 $sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
4 $result = mysql_query($sql2);
5
6 if($result == 0)
7 {
8 $sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = $userid 9 AND `PASSWORD` = $password";
10 mysql_query($sql3);
11 }


On line 4, the script sets a new value for $result. It looks in the database to see if the user has been activated. Even if I had $result 2 times, the script would work, because it sets a new value to $result, and that value should be zero. If it isn't, the script displays a message stating that the user account has already been activated.

Also, if I had what you put for the 'wrong code' it works. Here is a test using the same logic:

$result = 4;
if($result == 4)
$result == 3;
if (result ==3);
echo 'This script works.';
It DOES work.
http://www.netlinkhosting.com/see.php

When the code processes the first if(), the value for $result is 4. The script then changes the value for $result, and checks it again. Now, it equals 3, so the script displays 'This script works.'

96turnerri
02-06-2005, 12:46 PM
Originally posted by neenach2002
It DOES work.

there is nothing wrong with that logic therefore it would and does work, but you coded it differently on your demo, and have edited your post so we cant reference back to what it was as a change log o well.

Anyway, one thing i just did spot is this
if(result == 1){
surely that should be
if($result == 1){

or is that just a typo?

neenach2002
02-06-2005, 12:49 PM
That's the error!

Hard to spot, isn't it?

I typoed here, too:

$result = 4;
if($result == 4)
$result == 3;
if (result ==3);
echo 'This script works.';

I only changed the code to what I had in my most recently updated file.

It turns out I have two copies of this file on my harddrive. Both the same, except for the $result2 variable.

neenach2002
02-06-2005, 12:52 PM
Now the code errors that the account has already been activated, even if it hasn't.


<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['id']) && isset($_GET['code'])){
$userid = $_GET['id'];
$password = $_GET['code'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_num_rows(mysql_query($sql1));
if($result == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if($result2 == 0){
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3);
?>Your account has been activated. You can now login!<?;
}else{
?>
That account has already been registered. Please login <a href="index.php?page=login">here</a>.
<?
;}
}else{
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<?
;}
}else{
?>
Please make sure you click on the link in the e-mail that was sent to you.
<?;}

?>

96turnerri
02-06-2005, 01:03 PM
is that you most recent up to date, up to the minute code with no typos what so ever?

neenach2002
02-06-2005, 01:06 PM
That is the code I am using on the website, and the code I have edited for the error we just found.

96turnerri
02-06-2005, 01:16 PM
why is password row in caps when all others are lower? is that correct or an error?

WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";

i cant remember if MySQL is case sensitive, i think it is

to each of your queries make mysql_query($sql1) to mysql_query($sql1) or die(mysql_error()); then you will know if its a db error

neenach2002
02-06-2005, 01:30 PM
The PASSWORD table is in all caps in the database.

I have added the code you said, and I still get the message I was getting before.

The user has already been activated.

AdamBrill
02-06-2005, 08:52 PM
Originally posted by 96turnerri
i cant remember if MySQL is case sensitive, i think it isNo, MySQL is not case sensitive. "SELECT * FROM `test`;" will match the same as "SELECT * FROM `TEST`;" and "SELECT * FROM `tEsT`;".

neenach2002
02-06-2005, 08:55 PM
Any ideas why it's not working?

AdamBrill
02-06-2005, 09:18 PM
It is probably due to these lines:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if($result2 == 0){

My guess is they should look like this:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if(mysql_num_rows($result2) == 0){

neenach2002
02-06-2005, 09:31 PM
$result should equal 0 if the user is not activated.

AdamBrill
02-06-2005, 09:42 PM
Originally posted by neenach2002
$result should equal 0 if the user is not activated. Did you mean $result2?

Anyway, did you try what I suggested? If so, did it not work? This is what you had:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if($result2 == 0){

$results2 would always equal true unless there is an error in your SQL, since $result2 is not referring to how many results were returned.

Did you try what I suggested that you try? If not, please try what I suggested before posting back.

neenach2002
02-06-2005, 09:48 PM
Yes, I did. The same thing happened.
$result2 should equal the value in the field 'activation' in the table associated with the userid.

AdamBrill
02-06-2005, 10:05 PM
Originally posted by neenach2002
Yes, I did. The same thing happened.
$result2 should equal the value in the field 'activation' in the table associated with the userid. This is wrong. If you wanted to actually get the results from your query, you would have to use mysql_fetch_assoc(or a similiar function) not just mysql_query.

Anyway, if you have this:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";
$result2 = mysql_query($sql2);
if(mysql_num_rows($result2) == 0){

and you are getting the }else{ message:

That account has already been registered. Please login <a href="index.php?page=login">here</a>.

then that would mean that this query is returning at least one results(or is causing a mysql error):

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid'";

So, as another guess(I can only guess at the problem since I don't have access to your whole system), I would suggest changing your query to as follows:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid' AND `activated` = 'true'";

Assuming that `activated` equals 'true' after the user has been activated.

Also please note that the other changes that I suggested before should also still be in place...

neenach2002
02-06-2005, 10:33 PM
I get the following error:
Query was empty
New code:

<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['id']) && isset($_GET['code'])){
$userid = $_GET['id'];
$password = $_GET['code'];
$sql1 = "SELECT COUNT(*) FROM users WHERE userid = '$userid' LIMIT 1";
$result = mysql_num_rows(mysql_query($sql1));
if($result == 1){
$$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid' AND `activated` = '0'";
$result2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($result2) == 0){
$sql3 = "UPDATE `users` SET 'activated' = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3) or die(mysql_error());
?>Your account has been activated. You can now login!<?;
}else{
?>
That account has already been registered. Please login <a href="index.php?page=login">here</a>.
<?
;}
}else{
?>
You did not supply a valid user id. Please click the link in the e-mail that was sent to you again.
<?
;}
}else{
?>
Please make sure you click on the link in the e-mail that was sent to you.
<?;}
?>

AdamBrill
02-07-2005, 07:18 AM
This:

$$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid' AND `activated` = '0'";

Should be this:

$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid' AND `activated` = '1'";

Notice the $$ was changed to $ and the 0 was changed to 1.

neenach2002
02-07-2005, 12:02 PM
I discovered I could remove part of my code. Here is my new code.
It now says 'Your account has been activated. You can now login!', but the database does not change.

Hey Adam, if you want to use this as a pre-made script once we fix the error, I'll send you the files!
<?
include 'db.php';
?>
<div align="center" class="content">
<?
if(isset($_GET['id']) && isset($_GET['code'])){
$userid = $_GET['id'];
$password = $_GET['code'];
$sql1 = "SELECT COUNT(*) FROM `users` WHERE `userid` = '$userid' LIMIT 1";
$result = mysql_query($sql1);
if(mysql_num_rows($result) == 1){
$sql2 = "SELECT `activated` FROM `users` WHERE `userid` = '$userid' AND `activated` = '1'";
$result2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($result2) == 0){
$sql3 = "UPDATE `users` SET `activated` = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";
mysql_query($sql3) or die(mysql_error());
?>Your account has been activated. You can now login!<?;
}else{
?>
That account has already been registered. Please login <a href="index.php?page=login">here</a>.
<?
;}
}
}else{
?>
Please make sure you click on the link in the e-mail that was sent to you.
<?;}
?>

AdamBrill
02-07-2005, 01:09 PM
My guess is that this query doesn't match any rows:

$sql3 = "UPDATE `users` SET `activated` = 1 WHERE `userid` = '$userid' AND `PASSWORD` = '$password'";

I would suggest changing it to this instead if `userid` is an auto increment field so it will always be unique, which I'm assuming it is.

$sql3 = "UPDATE `users` SET `activated` = '1' WHERE `userid` = '$userid';";

Good luck! BTW, what does this complete script do?

neenach2002
02-07-2005, 01:39 PM
It's a member system that I designed. I just finished editing it so that you can edit all settings in the config file...such as website name and admin e-mail!...=)

It still doesn't work....it still echoes that the accoutn has been activated, but it doesn't change anything in the database...:(

AdamBrill
02-07-2005, 05:18 PM
Ok, a couple of things...

1. This:

$sql1 = "SELECT COUNT(*) FROM `users` WHERE `userid` = '$userid' LIMIT 1";
$result = mysql_query($sql1);
if(mysql_num_rows($result) == 1){

Should be this:

$sql1 = "SELECT * FROM `users` WHERE `userid` = '$userid' LIMIT 1";
$result = mysql_query($sql1);
if(mysql_num_rows($result) == 1){

2. If running this query isn't setting the activated field to 1:

UPDATE `users` SET `activated` = '1' WHERE `userid` = '$userid';

then you may want to try making sure there is a `userid` of the value of $userid. If there is, then maybe try printing out the sql and running it in phpMyAdmin and see if it works there. Other than that it should be working as long as that section is running, which it must be if you are getting the "Your account has been activated. You can now login!" message.

neenach2002
02-07-2005, 05:53 PM
It works....YIPPEE!!!!!!!

Thanks Adam...

If you want it for your site as a downloadable script, let me know...=)

96turnerri
02-07-2005, 08:37 PM
edit all settings in the config file

wouldnt it be better to create an options form page where they can change these variables

neenach2002
02-07-2005, 08:40 PM
Once I have completed the installer script, it will be included there. For now, it's by hand.

vBulletin has you edit everything by hand!