Clinton Posted January 2, 2009 Share Posted January 2, 2009 unexpected T_ELSE. Any ideas? (I /// HERE HERE /// the code in question) <?php /// LOGIN START /// /// CHECK TO SEE IF USER IS LOGGED IN /// if($_SESSION['logged_in'] == TRUE) { $query = mysql_query('SELECT prefix, lname FROM employers WHERE id = "'.mysql_real_escape_string($_SESSION['user_id']).'"'); $row = mysql_fetch_assoc($query); extract($row); ?> <div class="module_s2"> <div class="first"> <div class="sec"> <h2><? echo $prefix; echo $lname; ?></h2> <div class="box-indent"> <div class="width"> Welcome back. You have the con.<br /> <p> <a href="logout.php">Logout</a> </p> </div> </div> </div> </div> </div> <?php } /// IF USER IS NOT LOGGED IN THEN CHECK TO SEE IF FORM IS SUBMITTED /// elseif (isset($_POST['login'])) { if($_POST['username']!='' && $_POST['password']!='') { $query = mysql_query('SELECT id, username, active FROM users WHERE username = "'.mysql_real_escape_string($_POST['username']).'" AND password = "'.mysql_real_escape_string(md5($_POST['password'])).'"'); if(mysql_num_rows($query) == 1) { $row = mysql_fetch_assoc($query); if($row['active'] == NULL) { $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = TRUE; header("Location: index.php"); } ?> echo "<div class='module_s2'>"; echo "<div class='first'>"; echo "<div class='sec'>"; echo "<h3>Login</h3>"; echo "<div class='box-indent'>"; echo "<div class='width'>"; <?php /// HERE HERE HERE HERE /// else { $error = 'Your membership was not activated. Please open the email that we sent and click on the activation link'; } } else { $error = 'Login failed !'; } } else { $error = 'Please user both your username and password to access your account'; } } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="login" class="form-login"> <label> Username </label> <input type="text" name="username" class="inputbox"/> <input type="text" name="username" class="inputbox"/> <label> Password </label> <input type="password" name="password" class="inputbox" /><br /> <input type="checkbox" name="remember" class="checkbox" value="yes" /> <label class="remember"> Remember Me </label> <input type="submit" name="login" class="button" value="login" /> <p> <a href="passwordreset.php"> Forgot your password? </a></p> <p> <a href="usernamefind.php"> Forgot your username?</a> </p> <p> No Account Yet? <a href="register.php"><br /> Create an account</a> </p> </form> </div> </div> </div> </div> </div> <?php else { ?> echo "<div class='module_s2'>"; echo "<div class='first'>"; echo "<div class='sec'>"; echo "<h3>Login</h3>"; echo "<div class='box-indent'>"; echo "<div class='width'>"; <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="login" class="form-login"> <label> Username </label> <input type="text" name="username" class="inputbox"/> <input type="text" name="username" class="inputbox"/> <label> Password </label> <input type="password" name="password" class="inputbox" /><br /> <input type="checkbox" name="remember" class="checkbox" value="yes" /> <label class="remember"> Remember Me </label> <input type="submit" name="login" class="button" value="login" /> <p> <a href="passwordreset.php"> Forgot your password? </a></p> <p> <a href="usernamefind.php"> Forgot your username?</a> </p> <p> No Account Yet? <a href="register.php"><br /> Create an account</a> </p> </form> </div> </div> </div> </div> </div> <?php } ?> <?php /////END LOGIN//// ?> Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/ Share on other sites More sharing options...
btherl Posted January 2, 2009 Share Posted January 2, 2009 Which part of the if/else does the text just before the error belong in? Try moving it inside the appropriate one. Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-727936 Share on other sites More sharing options...
sasa Posted January 2, 2009 Share Posted January 2, 2009 you can't break php code between if and else blocks <?php if (blah){ //some code } ?><?php // you can't break php code here else { //more code } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-727940 Share on other sites More sharing options...
kenrbnsn Posted January 2, 2009 Share Posted January 2, 2009 you can't break php code between if and else blocks Yes, you can. I used to do this all the time, until it got too hard to follow the logic when trying to debug. Ken Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-728048 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 Change else { to } else { In your ///HERE HERE HERE HERE/// line [edit] Or myabe not... It seems ok, when I pasted it into my editor But man... You've got some php outside php tags just above this line Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-728058 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 You can't have code in between the closing brace of an if and the start of an else statement. Example: <?php if (1 == 1) { print "One = one, that's good...\n"; } ?> Just some random output, like your random HTML... <?php else { Error: Parse error: syntax error, unexpected T_ELSE in /home/nick/- on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-728060 Share on other sites More sharing options...
Clinton Posted January 2, 2009 Author Share Posted January 2, 2009 Ok, so I changed everything in between brackets but I can't get this to work properly. This form submits to itself. So, if the user come to the website it displays the login form (the third step in the script). If they submit the form but are currently not logged in they go to the second step. I had to take out header("Location: index.php"); because they were already being called. Then if the index.php page is called and they are logged in it is supposed to show them being logged in and the login box is supposed to disappear. Well... 1) It pulls up the page just fine if somebody is not logged in. 2) If I try to log in with my username only I get no errors. It looks like I just didn't do anything. 3) If I enter my username and password it doesn't find any rows so I get an extract row error but it does show the logged in portion of the script. Should I just use a seperate login.php form to submit this too instead of submitting form to self? Would that solve these problems? <?php } /// IF USER IS NOT LOGGED IN THEN CHECK TO SEE IF FORM IS SUBMITTED /// elseif (isset($_POST['login'])) { if($_POST['username']!='' && $_POST['password']!='') { $query = mysql_query('SELECT id, username, active FROM users WHERE username = "'.mysql_real_escape_string($_POST['username']).'" AND password = "'.mysql_real_escape_string(md5($_POST['password'])).'"'); if(mysql_num_rows($query) == 1) { $row = mysql_fetch_assoc($query); if($row['active'] == NULL) { $_SESSION['user_id'] = $row['id']; $_SESSION['logged_in'] = TRUE; }else { $error = 'Your membership was not activated. Please open the email that we sent and click on the activation link'; } } else { $error = 'Login failed !'; } } else { $error = 'Please user both your username and password to access your account'; } ?> <div class='module_s2'> <div class='first'> <div class='sec'> <h3>Login</h3> <div class='box-indent'> <div class='width'> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="login" class="form-login"> <label> Username </label> <input type="text" name="username" class="inputbox"/> <label> Password </label> <input type="password" name="password" class="inputbox" /><br /> <input type="checkbox" name="remember" class="checkbox" value="yes" /> <label class="remember"> Remember Me </label> <input type="submit" name="login" class="button" value="login" /> <p> <a href="passwordreset.php"> Forgot your password? </a></p> <p> <a href="usernamefind.php"> Forgot your username?</a> </p> <p> No Account Yet? <a href="register.php"><br /> Create an account</a> </p> </form> </div> </div> </div> </div> </div> <?php } else { ?> <div class='module_s2'> <div class='first'> <div class='sec'> <h3>Login</h3> <div class='box-indent'> <div class='width'> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="login" class="form-login"> <label> Username </label> <input type="text" name="username" class="inputbox"/> <label> Password </label> <input type="password" name="password" class="inputbox" /><br /> <input type="checkbox" name="remember" class="checkbox" value="yes" /> <label class="remember"> Remember Me </label> <input type="submit" name="login" class="button" value="login" /> <p> <a href="passwordreset.php"> Forgot your password? </a></p> <p> <a href="usernamefind.php"> Forgot your username?</a> </p> <p> No Account Yet? <a href="register.php"><br /> Create an account</a> </p> </form> </div> </div> </div> </div> </div> <?php } ?> <?php /////END LOGIN//// ?> Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-728167 Share on other sites More sharing options...
Clinton Posted January 2, 2009 Author Share Posted January 2, 2009 I stopped submitting the form to self and just created a form elsewhere. Problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/139176-solved-unexpected-t_else-yes-i-googled-it/#findComment-728410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.