Gah Another problem :S (Sessions maybe)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Gah Another problem :S (Sessions maybe)

Post by ericburnard »

Hey again. I decided to start learning about sessions today seing as everywhere says you should learn them first. with trying to do this on my mbers page i came up with a problem of two databases open in one bit of php code.

the code if quite long but you only really nead the top bit i think. here it is

Code: Select all

<?php 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 

include('http://eric.madashatters.com/header.inc');

if($_SESSION['logedin']){ 

$link = mysql_connect('localhost', '*******', '*************') 
   or die('Could not connect: ' . mysql_error()); 
mysql_select_db('madashat_mail') or die('Could not select database'); 

$username = mysql_real_escape_string($_GET['user']); 
$mail = $_GET['mail']; 
$query = "SELECT * FROM pm WHERE user='$username' AND new='New'"; 
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
  $i++; 
} 
echo "<center>You have " . $i . " new messages<BR><BR>"; 

mysql_close($link)  // I think this is what the prblem is

// Connecting, selecting database 
mysql_connect('localhost', '*******', '*******')   // Even though it says its on this line
   or die('Could not connect: ' . mysql_error()); 
mysql_select_db('madashat_users') or die('Could not select database');  

$username = mysql_real_escape_string($_GET['user']); 
$query2="SELECT * FROM users WHERE username = '$username'";
$result2=mysql_query($query2);
$num=mysql_numrows($result2);

echo "<table cellpadding=10><tr><td align='left'><BR>
<div class='white'><center>
(the code continues)
I no this is prob really simple but im stuck :S lol

any help would be great
Thanks
Eric
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

// Connecting, selecting database
mysql_connect('localhost', 'username', 'password')   // Even though it says its on this line
   or die('Could not connect: ' . mysql_error());
mysql_select_db('db1') or die('Could not select database');
whats the error generated by mysql_error() ?
Last edited by anjanesh on Sat Sep 03, 2005 9:42 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're guess is correct.. mysql_close() needs a semi-colon to finish the statement. ;)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Test the value of

$_SESSION['logedin']

???


What is this supposed to be set to, and what is it actually set to. Try using var_dump to see what it holds, and ensure its a valid value. I assume it should be either a true/false boolean value?

Just make certain there is not the most obvious error - trying to evaluate a string of value "false" as false. Any string with content will return true when the sole condition of an if statement.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

Code: Select all

Parse error: parse error, unexpected T_STRING in /home/madashat/public_html/eric/nm.php on line 26
i forgot to take out my username and password but have done it in my post noe. you were obviously very quick to respond and wissed the cange :p could you take it out of your post please.

Thank you :D

Eric
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

Thanks Feyd :D but now im getting the error

Code: Select all

Parse error: parse error, unexpected ':' in /home/madashat/public_html/eric/nm.php on line 74
which is part of the header (location: bit

Maugrim

The tutorial i am using shows this for the if function bit

Code: Select all

<?php 
// start the session 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
$_SESSION = array(); 
session_destroy(); 
echo "<strong>Step 5 - Destroy This Session </strong><br />"; 
if($_SESSION['name']){ 
    echo "The session is still active"; 
} else { 
    echo "Ok, the session is no longer active! <br />"; 
    echo "<a href=\"page1.php\"><< Go Back Step 1</a>"; 
} 
?>
It dosnt use any boolien so i just assumed that it ment "if the session is there then......."

:S am i wrong does it need to be

Code: Select all

if($_SESSION['name'] = TRUE;) {
??

Thanks again
Eric
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Could you post lines 64-84 ?
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

Code: Select all

<tr>
<td><br><center><div class='white'>Welcome back";

echo $_SESSION['susername'];

echo "!!!<br><br>
This is your main members page which will be displayed everytime you login. Over on the right there is your main navigation menu for travling through the members area. <br><br>Also in the next few weeks i will get around to making a bar along the top (just underneath the main navigation bar) where you can see who is online and how many new email messages you have in your have in your inbox.<br><BR>Enjoy<br> Eric x x x <BR></td></tr></table></td></tr></table>";

} else {

header (location: http://eric.madashatters.com/login.html); // Line 74

}
?>


<BR><BR>
<?
include('http://eric.madashatters.com/footer.inc')
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

can you post the entire string involved in the first part that got truncated?
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

I didnt quite understand what you were asking for so her is the whols script :s sorry

Code: Select all

<?php 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 

include('http://eric.madashatters.com/header.inc');

if($_SESSION['logedin']){ 

// Connecting, selecting database 
$link = mysql_connect('localhost', 'madashat_eric', 'gateway2') 
   or die('Could not connect: ' . mysql_error()); 
mysql_select_db('madashat_mail') or die('Could not select database'); 

$username = mysql_real_escape_string($_GET['user']); 
$mail = $_GET['mail']; 
$query = "SELECT * FROM pm WHERE user='$username' AND new='New'"; 
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
  $i++; 
} 
echo "<center>You have " . $i . " new messages<BR><BR>"; 

mysql_close($result);

// Connecting, selecting database 
mysql_connect('localhost', 'madashat_eric', 'gateway2') 
   or die('Could not connect: ' . mysql_error()); 
mysql_select_db('madashat_users') or die('Could not select database');  

$username = mysql_real_escape_string($_GET['user']); 
$query2="SELECT * FROM users WHERE username = '$username'";
$result2=mysql_query($query2);
$num=mysql_numrows($result2);

echo "<table cellpadding=10><tr><td align='left'><BR>
<div class='white'><center>

<br><p align='left'>
<table style='border: dotted 1px'>
<tr>
<td width=270><center><u><div class='white'>Your Menu</u>
</td>
</tr>
<tr>
<td><br><center><a href='http://eric.madashatters.com/mail.php?user=$username'>Your Inbox</a>
<br>
<a href='http://www.madashatters.com/eric/mpupload.php?user=$username'>Upload Pictures</a>
<br>
<a href='http://www.madashatters.com/eric/menu.php?page=photos&gallery=$username'>View Your Gallery</a>
<br>
<a href='http://www.madashatters.com/eric/eblog.php?user=$username'>Edit Your Blog</a>
<br>
<a href='http://www.madashatters.com/eric/mblog2.php?user=$username'>View Your Blog</a>
<br>
<a href='http://www.madashatters.com/eric/profiles.php?user=$username'>Profiles</a>
<br><br>
</td></tr></table>
</td><td align='right'>
<p align='right'><table style='border: dotted 1px width=420'>
<tr>
<td><u><div class='white'></u>
</td>
</tr>
<tr>
<td><br><center><div class='white'>Welcome back";

echo $_SESSION['susername'];

echo "!!!<br><br>
This is your main members page which will be displayed everytime you login. Over on the right there is your main navigation menu for travling through the members area. <br><br>Also in the next few weeks i will get around to making a bar along the top (just underneath the main navigation bar) where you can see who is online and how many new email messages you have in your have in your inbox.<br><BR>Enjoy<br> Eric x x x <BR></td></tr></table></td></tr></table>";

} else {

header (location: http://eric.madashatters.com/login.html);

}
?>


<BR><BR>
<?
include('http://eric.madashatters.com/footer.inc')
?>

Sorry :S
Eric
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

header ('Location: http://eric.madashatters.com/login.html');
Post Reply