PDA

View Full Version : session not working in php


Pyscho
04-09-2005, 06:42 PM
This is the php code segment from my login.php. It starts a session when login is successful..

<?php
$sqlserv = "localhost";
$sqluser = "root";
$sqlpass = "";
$sqldb = "testdb";

$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) || empty($password)) { print("<h3>Login unsuccessful: You need to submit both your username and password.</h3>"); }
else {
global $sqlserv, $sqluser, $sqlpass, $sqldb;
$link = mysql_connect($sqlserv, $sqluser, $sqlpass) or die ("MYSQL Connection Error: ".mysql_error());
@mysql_select_db($sqldb) or die ("MYSQL Database Error: ".mysql_error());

$result = mysql_query("SELECT username FROM user WHERE username='$username' AND password='$password'") or die("DB ERROR" . mysql_error());
$num_rows = mysql_num_rows($result);
if (($num_rows)==0) { print("<h3>Login unsuccessful: No such username exists or username/password combination incorrect.</h3>"); }

else {
print("<h3>Welcome"); print (" " . $username); print (", enjoy your stay.</h3>");
session_start();
session_register('username');
}
}
?>


And then, i have another file called product.php which basically lists all the products in the form of a table..


<?php

if (session_is_registered('username')) {

$sqlserv = "localhost";
$sqluser = "root";
$sqlpass = "";
$sqldb = "testdb";

global $sqlserv, $sqluser, $sqlpass, $sqldb;
$link = mysql_connect($sqlserv, $sqluser, $sqlpass) or die ("MYSQL Connection Error: ".mysql_error());
@mysql_select_db($sqldb) or die ("MYSQL Database Error: ".mysql_error());

$result = mysql_query("SELECT * FROM product") or die("DB ERROR" . mysql_error());

while ($row = mysql_fetch_array($result)) {
echo "
<tr align=center class=column-row-1>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
";
}

}

else { print ("You must log in first!"); }

?>


But my problem is, i only want the table in products.php to appear *IF* the user is logged in (i.e. if the session exists) , otherwise it should display a message on the products.php page saying 'login first' etc.
I've tried this, as you can see from the code, but it just doesn't work. Anyone see what im doing wrong?

bfsog
04-09-2005, 07:23 PM
Use $_SESSION.

Here is code from a script I done.

login.php

$sql="SELECT * FROM users WHERE username='".$username."' AND password='".md5($pwd)."'";
$res=mysql_query($sql) or die("Error: ".mysql_error());
if(mysql_num_rows($res)>0)
{
$_SESSION['name'] = $username;
header("Location: admin.php");
}


the above code checks attempts to log them in. now in admin.php this is how i check if they are logged in..


<?php

if (!isset($_SESSION['name'])) {

echo 'You have to sign in first ';
echo '<a href="login.php" title="Login">Login</a>';

}

?>


Hope this helps.

Pyscho
04-09-2005, 07:42 PM
hey thanks, but it doesnt work:

in my login.php the code segment where i put the session in is:


print("<h3>Welcome"); print (" " . $username); print (", enjoy your stay.</h3>");
$_SESSION['name'] = $username;
print("Session started");
}


and then the other page is product.php:


<?php

if (isset($_SESSION['name'])) {


$sqlserv = "localhost";
$sqluser = "root";
$sqlpass = "";
$sqldb = "testdb";

global $sqlserv, $sqluser, $sqlpass, $sqldb;
$link = mysql_connect($sqlserv, $sqluser, $sqlpass) or die ("MYSQL Connection Error: ".mysql_error());
@mysql_select_db($sqldb) or die ("MYSQL Database Error: ".mysql_error());

$result = mysql_query("SELECT * FROM product") or die("DB ERROR" . mysql_error());

while ($row = mysql_fetch_array($result)) {
echo "
<tr align=center class=column-row-1>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
";
}

}

else { echo 'You have to login in first '; }

?>


The thing is, when i log in with a correct username/password, i get the welcome message as usual, but then when i click on the product link(in the sidebar) to view the product table it says 'You have to login in first' .
When i log in, it should display the table, if im not logged in then it should only display this message. what am i doing wrong?

DigitalBliss
04-09-2005, 07:47 PM
do you have session_start()???

bfsog
04-09-2005, 07:49 PM
Yes, every page that you wish to use sessions, have session_start();

That should be you on your way.

Pyscho
04-09-2005, 07:53 PM
ok i added in the session start and now it logs in, in the login.php but i get a weird error:


Welcome john, enjoy your stay.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\login.php:1) in c:\wamp\www\login.php on line 56

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\wamp\www\login.php:1) in c:\wamp\www\login.php on line 56
Session started


heres the code segment

else {
print("<h3>Welcome"); print (" " . $username); print (", enjoy your stay.</h3>");
session_start();
$_SESSION['name'] = $username;
print("Session started");
}

DigitalBliss
04-09-2005, 07:53 PM
I made a little function to check if my users are logged in:


function auth($type){

if($type == "admin"){
$authadmin = $_SESSION['admin'];
if($authadmin != ""){ return true; }
else{return false;}
}

if($type == "user"){
$authuser = $_SESSION['login'];
if($authuser != ""){ return true; }
else{return false;}
}

}


Then where you want to check it:


if(!auth("user")){
//User isnt logged in
}

Pyscho
04-09-2005, 08:01 PM
im getting all sorts of errors, this one is in my login one when i actually log in with a correct username/password:

Welcome john, enjoy your stay.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\login.php:1) in c:\wamp\www\login.php on line 56

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\wamp\www\login.php:1) in c:\wamp\www\login.php on line 56
Session started


and then when i clock on the product link to view the product table (the table shud appear becasue i logged in as john correctly) i get..


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\wamp\www\product.php:1) in c:\wamp\www\product.php on line 38

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\wamp\www\product.php:1) in c:\wamp\www\product.php on line 38
You have to login in first


the table doesnt appear, too many errors, please help :confused:

bfsog
04-09-2005, 08:01 PM
Make sure at the top of every page that you do a session check that you have


session_start();

Pyscho
04-09-2005, 08:10 PM
excellent, thanks very much to all you guys, as bfsog said, all i need at the top of the necessary pages was


<?php
session_start();
?>


thanks everyone!

Also, for logging out, do i just do session_destroy ?

DigitalBliss
04-09-2005, 08:38 PM
Lol I asked you if you had that ;) use unset($_SESSION['SESSIONNAME']);

Pyscho
04-09-2005, 09:04 PM
thank you mate, much appreciated!

by the way when i have something like this:


<tr class="header-rows">
<th>Product number</th>
<th>Product name</th>
<th>Price</th>
<th>Stock level</th>
</tr>


and i only want that to display if theres a session, this doesnt work: :confused:


if (isset($_SESSION['name'])) {
<tr class="header-rows">
<th>Product number</th>
<th>Product name</th>
<th>Price</th>
<th>Stock level</th>
</tr>
}

DigitalBliss
04-09-2005, 09:07 PM
Haha thats cuz your code says if there isnt a session show it :P


if (!isset($_SESSION['name'])) {
<tr class="header-rows">
<th>Product number</th>
<th>Product name</th>
<th>Price</th>
<th>Stock level</th>
</tr>
}


Add a ! before the isset

Pyscho
04-09-2005, 09:12 PM
huh lol. i mean if the session exists, then show it, if the session doesnt exists, don't show it..


if (isset($_SESSION['name'])) {
<tr class="header-rows">
<th>Product number</th>
<th>Product name</th>
<th>Price</th>
<th>Stock level</th>
</tr>
}


So that is saying if the session 'name' exists, show it. but it doesnt work, when i dont log in, and i click on the product page, i still see it.

Pyscho
04-09-2005, 09:25 PM
ok never mind, got it working:


<?php

if (isset($_SESSION['name'])) {

echo "
<tr class=\"header-rows\">
<th>Product number</th>
<th>Product name</th>
<th>Price</th>
<th>Stock level</th>
</tr>
";

$sqlserv = "localhost";
$sqluser = "root";
$sqlpass = "";
$sqldb = "testdb";

global $sqlserv, $sqluser, $sqlpass, $sqldb;
$link = mysql_connect($sqlserv, $sqluser, $sqlpass) or die ("MYSQL Connection Error: ".mysql_error());
@mysql_select_db($sqldb) or die ("MYSQL Database Error: ".mysql_error());

$result = mysql_query("SELECT * FROM product") or die("DB ERROR" . mysql_error());

while ($row = mysql_fetch_array($result)) {
echo "
<tr align=center class=column-row-1>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
";
}

}

else { print ("<h3>You don't have permission to view the products table, please log in.</h3>"); }


?>


but can someone tell me though how i can centre the text
'You don't have permission to view the products table, please log in'
(its at the bottom of the code)

Velox Letum
04-10-2005, 01:27 AM
Use the center html tag.


else { print ("<center><h3>You don't have permission to view the products table, please log in.</h3></center>"); }

DigitalBliss
04-10-2005, 02:21 AM
This would also work:


else { print ("<div align=\"center\"><h3>You don't have permission to view the products table, please log in.</h3></div>"); }

Pyscho
04-10-2005, 02:42 AM
thanks dbliss, it worked, the <center> one didnt tho, but thans for the reply anyway.

barkermn01
12-22-2007, 02:14 AM
can some one help me i get these errors
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home1/ffd/public_html/index.php:1) in /home1/ffd/public_html/config/session.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home1/ffd/public_html/index.php:1) in /home1/ffd/public_html/config/session.php on line 2

From my index.php witch open all other pages inside its self and at the verry top of the file nothing befor it it has

<?php
require_once("config/session.php");
?>

and my session just has

<?php
session_start();
?>

Inigoesdr
12-22-2007, 02:18 AM
You have a space or some other characters before the <?php tag at the beginning of index.php.

barkermn01
12-22-2007, 05:34 PM
Nop this there is no space or anything
like that

CFMaBiSmAd
12-22-2007, 05:49 PM
index.php is probably saved as an UTF-8 encoded file and has the BOM (byte order marker) as the first few characters of the file.

Either save the file as ANSI/ASCII encoded file or if you are FTP'ing it to your server, transfer it in ASCII mode instead of binary mode.

barkermn01
12-22-2007, 06:11 PM
Thanks for that it worked other then i need the Code for a copyright simbole pls

PappaJohn
12-22-2007, 06:33 PM
&copy;

SeeIT Solutions
12-23-2007, 12:18 AM
Oops, didn't see the 2nd page...