Jump to content

Archived

This topic is now archived and is closed to further replies.

treilad

What's wrong with this code?

Recommended Posts

Not sure I should go into detail if it's just a syntax error, but I will if needed.

The error is:

[quote]
Parse error: parse error, unexpected T_ELSE in C:\Server\wamp\www\project\index.php on line 17[/quote]

Code:
[code]<?php

if(isset($_COOKIE['ID_my_site']))

{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )){


  if (!isset($_COOKIE['ID_my_site']))
  {
    header('./logout.php')


else
{
  header('./loginpage.php')
}

?>[/code]

It doesn't like the 'else', but I don't know why. :-/

Share this post


Link to post
Share on other sites
You have a missing ; and } after this:
[code=php:0]if (!isset($_COOKIE['ID_my_site']))
  {
    header('./logout.php')[/code]


it should be:
[code=php:0]if (!isset($_COOKIE['ID_my_site']))
{
    header('./logout.php'); // missing
} // missing[/code]

Share this post


Link to post
Share on other sites
You're missing a } to end your while loop and your if statement.  You need to close your if and while brackets.

Share this post


Link to post
Share on other sites
You need a curly right brace before the else. Don't forget semi-colons after each header. And a curly right brace to close off the while loop.

Ask yourself why you're checking the cookie variable within the while loop? also if that's the correct form of use of the header() function?

Please read some more tutorials on basic PHP syntax and use php.net to help yourself learn what header() does.  I think you meant to use include/require.

Share this post


Link to post
Share on other sites
What about the indenting? It looks like the else should be for the first If statement, which would mean that the first if statment, the while loop, and the second if statement have not been closed.

Share this post


Link to post
Share on other sites
I meant to header, because this is just a script to check the credentials of the user. Each page loads it automatically (ideally, anyway. working on that.) to check their credentials, and if they check out, they can view the page.

EDIT:

I said that wrong. I have this at the top of each page. It checks their credentials and if they check out, it does nothing. If the credentials don't match, it removes the cookie because they have a cookie set but their credentials don't match, so they don't keep the cookie. If there isn't a cookie, it boots them from the page to the login page.

Also, now I'm getting this error:

[quote]
Parse error: parse error, unexpected $end in C:\Server\wamp\www\project\index.php on line 204[/quote]

204 is the end. Nothing there, so I assume I'm missing something.

Share this post


Link to post
Share on other sites
You are missing  closing } or ) if you are getting that error. Go through you code making sure your } and ) pair up.

Share this post


Link to post
Share on other sites
<?php

if(isset($_COOKIE['ID_my_site'])) {

$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];

$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) {


  if (!isset($_COOKIE['ID_my_site'])) {
    header('./logout.php')

}else{
 
header('./loginpage.php')
}
}

?>

Share this post


Link to post
Share on other sites
It didn't like yours, redarrow. :( But I got it to work with wildteens tip.

Also, the code cleared up. No more errors. But I can still visit the page when I'm logged out. I have include('index3.php'); which is the code I just fixed at the top of index.php. Before I make another topic, I'll just put it here and see if there's a simple answer. I need the code to ensure that the page is not visible if the user's credentials do not match up. For some reason it doesn't header me anywhere even when I'm not logged in, as it should. Again, here's my "corrected" code:

[code]<?php

include ('db.php');

if(isset($_COOKIE['ID_my_site']))

{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{


  if (!isset($_COOKIE['ID_my_site']))
  {
    header('./logout.php');
  }
}
}
else
{
  header('./loginpage.php');
}

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.