Jump to content

What's wrong with this code?


treilad

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. :-/
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
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')
}
}

?>
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.