Web Hosting Talk







View Full Version : [PHP Help]


JustinSmall
01-31-2008, 02:31 PM
I'm good with php, I really am, I just can't figure out this error.


ERROR:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/lanced/public_html/clients/AReservationRightNow/livelogins.php on line 36


My PHP Code


<HTMl>
<head>
<script type="text/javascript" src="includes/mt.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
$('loginlive').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
</script>
</head>
<body>


<?
include 'connection.php';
if($_POST) {
sleep(2);
$username = $_POST[username];
$password = md5($_POST[pass]);
$info = mysql_query("SELECT * FROM members WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($_POST[username]==NULL|$_POST[pass]==NULL) {
echo 'Please fill in all fields';
} else {
if($password == $data[password]) {
setcookie("id", $data[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $data[password],time()+(60*60*24*5), "/", "");
echo'<form style=margin:0px;><INPUT TYPE=button VALUE=Reload onclick='location.reload()'></form>
<div>Thanks, You are now logged in! Click <b>reload</b> to get to the content you want</div>';
} else {
echo 'Incorrect username or password';
}
}
} else {
echo'Sorry, We cant accept that request.';
}
?>


</body>
</HTMl>

brendandonhu
01-31-2008, 02:47 PM
You need to escape the single-quotes within a single-quote enclosed string.

JustinSmall
01-31-2008, 02:54 PM
Can you show me where that is cause Im confused on what your saying lol

brendandonhu
01-31-2008, 03:00 PM
It's on line 36. You can't put single-quotes ('') inside another set of single-quotes. Change them to double-quotes (""), or escape them with a \.

larwilliams
01-31-2008, 03:30 PM
try this:


<html>
<head>
<script type="text/javascript" src="includes/mt.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
$('loginlive').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
</script>
</head>
<body>
<?php
include 'connection.php';
if($_POST) {
sleep(2);
$username = $_POST[username];
$password = md5($_POST[pass]);
$info = mysql_query("SELECT * FROM members WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($_POST[username]==NULL|$_POST[pass]==NULL) {
echo 'Please fill in all fields';
} else {
if($password == $data[password]) {
setcookie("id", $data[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $data[password],time()+(60*60*24*5), "/", "");
echo "<form style=margin:0px;><INPUT TYPE=button VALUE=Reload onclick='location.reload()'></form>
<div>Thanks, You are now logged in! Click <b>reload</b> to get to the content you want</div>";
} else {
echo 'Incorrect username or password';
}
}
} else {
echo'Sorry, We cant accept that request.';
}
?>
</body>
</html>

JustinSmall
01-31-2008, 03:32 PM
Great that worked, now I'm getting tihs





Warning: Cannot modify header information - headers already sent by (output started at /home/lanced/public_html/clients/AReservationRightNow/livelogins.php:6) in /home/lanced/public_html/clients/AReservationRightNow/livelogins.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /home/lanced/public_html/clients/AReservationRightNow/livelogins.php:6) in /home/lanced/public_html/clients/AReservationRightNow/livelogins.php on line 38

larwilliams
01-31-2008, 03:37 PM
That is because you can't send headers in PHP after anything is sent to the browser (HTML, unintended whitespaces, etc.).

Give me about 10 minutes. I'll look at it for you.

JustinSmall
01-31-2008, 03:40 PM
Awesome thanks!

It's using ajax to display underneath the form whether you are logged in or not...

Could that be sending double headers, if you have aim my AIM is appmonsterdotcom, I appreciate the help.

larwilliams
01-31-2008, 03:43 PM
Okay, I moved the relevant stuff to the top, before anything is sent to the browser and used the PHP isset() function to ensure the SQL query is only sent if the username and password have been supplied via $_POST.


<?php
include 'connection.php';
if(isset($_POST[username]) && isset($_POST[pass])){
$username = $_POST[username];
$password = md5($_POST[pass]);
$info = mysql_query("SELECT * FROM members WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($password == $data[password]) {
setcookie("id", $data[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $data[password],time()+(60*60*24*5), "/", "");
}
}
?>
<html>
<head>
<script type="text/javascript" src="includes/mt.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
$('loginlive').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
</script>
</head>
<body>
<?php
if($_POST) {
sleep(2);

if($_POST[username]==NULL|$_POST[pass]==NULL) {
echo 'Please fill in all fields';
} else {
if($password == $data[password]) {
echo "<form style=margin:0px;><INPUT TYPE=button VALUE=Reload onclick='location.reload()'></form>
<div>Thanks, You are now logged in! Click <b>reload</b> to get to the content you want</div>";
} else {
echo 'Incorrect username or password';
}
}
} else {
echo'Sorry, We cant accept that request.';
}
?>
</body>
</html>

JustinSmall
01-31-2008, 03:49 PM
willaims to do you have MSN or AIM? so I can talk to you about this in real time?

JustinSmall
01-31-2008, 04:03 PM
ohh I see where I'm going wrong.

I'm trying to check for the user to be logged in... with tihs right here...

<? if($logged) {
echo'<div>Sorry, It seems you are already logged in, Try reloading this page.</div>';
} else {
?>

but $logged isn't recorded anywhere is it?

How do I make the $logged function enabled once the user signs in correctly...

and yes... that fixed it williams.

larwilliams
01-31-2008, 04:11 PM
<? if(isset($_COOKIE['pass']) && $_COOKIE['pass'] != '') {
echo'<div>Sorry, It seems you are already logged in, Try reloading this page.</div>';
} else {
?>


Give that a whirl instead. That checks to see if the cookie contains the md5'd password (which should only exist when the login was successful) and should work for you (hopefully!)

JustinSmall
01-31-2008, 04:16 PM
w0rks, thanks guys!!!!

JustinSmall
01-31-2008, 04:19 PM
wait, how do I klil the cookie?

JustinSmall
01-31-2008, 04:30 PM
Ok, I swear this is the last question...

I have to user groups, I want to check if the user logged in is in teh admin or members?

how would I check it for that user...

if(isset($_COOKIE['pass']) && $_COOKIE['pass'] != '') {
if ($rank == "Admin"){

} else {
echo("you don't have access here");
}

}


like this? and how do I make it log out aswell?

JustinSmall
01-31-2008, 05:30 PM
Nvm, I did a mysql select :D

larwilliams
01-31-2008, 09:52 PM
Not a problem. Glad to help a fellow freelancer :)

Czaries
02-01-2008, 11:17 AM
I don't think anyone here explained the actual issue that you were having, so here it is, just in case you were curious:

The setcookie() function works by sending headers to the browser to let the client computer know to have a cookie ready before the page loads. Since you were sending other content first before you sent the header (The "<html><head> ..." etc.), the headers were finalized and sent to the browser already, because they must precede any content. Since you made a call to the setcookie() function AFTER you had already sent content (output started on line 6, according to the error), you got the error and PHP was unable to set the cookie.

The general rule of thumb here is that the headers ALWAYS have to be sent FIRST. There cannot even be one line of whitespace in between two PHP tags before headers are sent, because it will be seen as output to the browser, and will cause an error. This is why "larwilliams" moved that code to the top of the page, and that's why it works there.

larwilliams
02-01-2008, 11:22 AM
Your right, in a way.. the closest i came to explaining it was:


That is because you can't send headers in PHP after anything is sent to the browser (HTML, unintended whitespaces, etc.).

JustinSmall
02-02-2008, 11:23 AM
Awesome thanks!!!

and if I wanted to do a log out it would be as simple as...

setcookie("id", "",time(-1), "/", "");
setcookie("pass", "",time(-1), "/", "");

or would it be something else!?

and... correct me if I'm wrong, but cookies are separated by websites correct?

larwilliams
02-02-2008, 11:52 AM
That is correct. The code you are looking for is as follows:


<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
session_destroy();
?>

Codebird
02-02-2008, 12:48 PM
yes this should work if you have a rank field for each user in the database and you are fetching it before this script as for the COOKIE I think setting $_COOKIE['pass']=''; does the job but I'm not sure