...

View Full Version : PHP sessions



punkpuke
02-17-2004, 03:50 AM
Ok, this login script I've been working on finally works with cookies. But, now I want to make it work with a Session. I've never done a session before. But, I read a tutorial and tried my best to make it work. I have the problem Creating the Session and then seeing if they are logged in.



<?php
if ($_POST[user] && $_POST[pass]) {
$link = mysql_connect ("localhost","username","password");
$database = mysql_select_db("database_name");
//$sql = "SELECT * FROM users WHERE username=".$_POST['user'];
$sql = "select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'";
$result = mysql_query($sql);

if (mysql_error()) {
// print(mysql_error());
$login_error= true;
}
else { // There was no error
if ($data = mysql_fetch_array($result)) {
if ($data[id] > 0) { // I HAVE TROUBLE STARTING HERE
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION['user'] = $_POST['user'];
mysql_close($link);
header("Location: test.php");
}
else { $login_error= true; }
}
else {
$login_error= true;
}
}

}//End Of Submit Request


if ($login_error== true){
print("Login Error Page. Go Back.<br />");
}
else {
if($_SESSION['user']){
?>
You Are logged in as <?php $_SESSION['user']; ?>
<a href=test.php?logout=true><u>Logout</u></a>
<?php
}
else {
?>

<form action=test.php method=post name="login" style="margin:0;">
Username<br /><input type="text" name="user" size="15" maxlength="20" class="textinput">
<br />
Password<br /><input type="password" name="pass" size="15" maxlength="25" class="textinput">
<br />
<input type="submit" name="submit" value="Submit" class="submit">
</form>
<?php
}
}
?>

Scrowler
02-17-2004, 04:10 AM
if ($data = mysql_fetch_array($result)) {
if ($data[id] > 0) { // I HAVE TROUBLE STARTING HERE
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION['user'] = $_POST['user'];
mysql_close($link);
header("Location: test.php");
}
else { $login_error= true; }
}
else {
$login_error= true;
}
}


theres your problem, the function session_start() must be put on the first line of code, before anything gets output to the browser. put it on the first or second line (<?php on first) and it should work fine.

hope this helps

punkpuke
02-17-2004, 04:28 AM
Oh, thanks.

But, if I wanted to logout...

<a href=test.php?logout=true><u>Logout</u></a>

if ($logout == true){
// ?? how would I make the session disappear??
}

punkpuke
02-17-2004, 04:34 AM
Nevermind. I figured that out. But, It won't display if your logged in.





$name = $_POST[user];

if($_SESSION['user']){
?>
You Are logged in as <?php echo "$name<br />"; ?>
<a href=test.php?logout=true><u>Logout</u></a>
<?php
}
else {
?>

Celtboy
02-17-2004, 05:47 AM
what does the full code look like now?

punkpuke
02-17-2004, 06:04 AM
It looks like this without the HTML



<?php
if ($_POST[user] && $_POST[pass]) {
$name = $_POST[user];
$link = mysql_connect ("localhost","username","password");
$database = mysql_select_db("database_name");
//$sql = "SELECT * FROM users WHERE username=".$_POST['user'];
$sql = "select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'";
$result = mysql_query($sql);

if (mysql_error()) {
// print(mysql_error());
$login_error= true;
}
else { // There was no error
if ($data = mysql_fetch_array($result)) {
if ($data[id] > 0) { // I HAVE TROUBLE STARTING HERE
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION['user'] = $_POST['user'];
mysql_close($link);
header("Location: test.php");
}
else { $login_error= true; }
}
else {
$login_error= true;
}
}

}//End Of Submit Request


if ($login_error== true){
print("Login Error Page. Go Back.<br />");
}
else {
if($_SESSION['user']){
?>
You Are logged in as <?php echo"$name"; ?>
<a href=test.php?logout=true><u>Logout</u></a>
<?php
}
else {
?>

<form action=test.php method=post name="login" style="margin:0;">
Username<br /><input type="text" name="user" size="15" maxlength="20" class="textinput">
<br />
Password<br /><input type="password" name="pass" size="15" maxlength="25" class="textinput">
<br />
<input type="submit" name="submit" value="Submit" class="submit">
</form>
<?php
}
}
?>

Scrowler
02-17-2004, 08:18 AM
you forgot to move the session_start() like i told you :p

punkpuke
02-17-2004, 08:22 AM
no I didn't lol. I got it working anyway. I just forgot to "show" that I moved it. Everythings all set. Thanks.

Scrowler
02-17-2004, 08:28 AM
ok, i assumed you didnt because i saw it in the middle, and im pretty sure if php finds it there itll spit out a few errors.

btw np

punkpuke
02-17-2004, 08:44 AM
Yeah, I was the lazy moron who just copied the above script. (because I already filled the real one with HTML up the Ying Yang.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum