Connecting Tech Pros Worldwide Help | Site Map

start_session() help please

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 21st, 2007, 11:12 PM
fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
Default start_session() help please

Hi all, I am new to php and am having trouble with a script. I *think* the problem is with start_session or register_session. Here are the two scripts that I am using.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include("inc/config.php");
  3. $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!");
  4. $query = "SELECT * FROM clients WHERE name = '$name' AND password = PASSWORD('$password')";
  5. $result = mysql_db_query($database, $query, $connection);
  6. if (mysql_num_rows($result) == 1)
  7.     {
  8.     session_start();
  9.  
  10.     session_register("client_id");
  11.     session_register("client_name");
  12.     session_register("client_email");
  13.     session_register("client_ref");
  14.     session_register("client_title");
  15.     list($clientid, $name, $pass, $email, $ref, $title) = mysql_fetch_row($result);
  16.     $client_id = $clientid;
  17.     $client_name = $name;
  18.     $client_email = $email;
  19.     $client_ref = $ref;
  20.     $client_title = $title;
  21.  
  22.     header("Location: menu.php");
  23.     mysql_free_result ($result);    
  24.  
  25.     mysql_close($connection);
  26.     }
  27. else
  28.  
  29.     {
  30.     mysql_free_result ($result);    
  31.     mysql_close($connection);
  32.  
  33.     header("Location: index.htm");
  34.     exit;
  35.     }
  36. ?>
and this one.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. session_start();
  3. if(!session_is_registered("client_id"))
  4. {
  5. header("Location: index.htm");
  6. exit;
  7. }
  8. ?>
  9. <html>
  10. <link rel="stylesheet" href="inc/style.css" type="text/css">
  11.  
  12. <body bgcolor="#FFFFFF">
  13. <img src="inc/title.gif" width="308" height="82">
  14. <?
  15. if ($client_name !== 'admin')
  16. {
  17. ?>
  18. <h2>Hello <b> 
  19.   <? echo $client_name ?>
  20.   </b> </h2>
  21.   Here are your invoices:
  22.  
  23. <?
  24. include "inc/dbconnect.php";
  25. include ("inc/date.php");
  26. $result = mysql_query("SELECT * FROM invoices WHERE clientid = '$client_id' ORDER BY $param",$db);
  27. if (!$param) {
  28. $result = mysql_query("SELECT * FROM invoices WHERE clientid = '$client_id' ORDER BY id",$db);
  29. }
  30. echo "<p><table border=1 cellspacing=0 cellpadding=2 bordercolor=#eeeeee width=400>";
  31. echo "<tr align=top><td><b><a href='menu.php?param=id'>Invoice number</a></b></td><td><b><a href='menu.php?param=date'>Date</a></b></td><td><b><a href='menu.php?param=total'>Total</a></b></td><td><b><a href='menu.php?param=status'>Status</a></b></td><td>&nbsp;</td></tr>";
  32.  
  33. while ($row = mysql_fetch_array($result))
  34. {
  35.     $id = $row["id"];
  36.     $date = $row["date"];
  37.     $dateshow = fixDate($date);
  38.     $total = $row["total"];
  39.     $status = $row["status"];
  40.  
  41. if ($alternate == "1") { 
  42.     $color = "#ffffff"; 
  43.     $alternate = "2"; 
  44.     } 
  45.     else { 
  46.     $color = "#efefef"; 
  47.     $alternate = "1"; 
  48.     } 
  49.  
  50. echo "<tr valign=top bgcolor=$color><td>$id</td><td>$dateshow</td><td>$total</td><td>$status</td><td>[ <a href='invoice.php?id=$id'>view</a> ]</td></tr>";
  51. }
  52. echo "</table>";
  53.  
  54. }
  55. elseif ($client_name == 'admin')
  56. {
  57.  
  58. echo "<h2>admin options</h2>";
  59.  
  60. include "inc/dbconnect.php";
  61. include ("inc/date.php");
  62. $result = mysql_query("SELECT * FROM invoices ORDER BY $param",$db);
  63. if (!$param) {
  64. $result = mysql_query("SELECT * FROM invoices ORDER BY id",$db);
  65. }
  66. echo "<p><table border=1 cellspacing=0 cellpadding=2 bordercolor=#eeeeee width=600>";
  67. echo "<tr align=top><td><b><a href='menu.php?param=id'>Invoice number</a></b></td><td><b><a href='menu.php?param=clientid'>Client</a></b></td><td><b><a href='menu.php?param=date'>Date</a></b></td><td><b><a href='menu.php?param=total'>Total</a></b></td><td><b><a href='menu.php?param=status'>Status</a></b></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
  68.  
  69. while ($row = mysql_fetch_array($result))
  70. {
  71.     $id = $row["id"];
  72.     $clientid = $row["clientid"];
  73.     $clientfind = mysql_query("SELECT title FROM clients WHERE clientid = '$clientid'",$db);
  74.     $clienttitle = mysql_result($clientfind,0);
  75.     $date = $row["date"];
  76.     $dateshow = fixDate($date);
  77.     $total = $row["total"];
  78.     $status = $row["status"];
  79.  
  80. if ($alternate == "1") { 
  81.     $color = "#ffffff"; 
  82.     $alternate = "2"; 
  83.     } 
  84.     else { 
  85.     $color = "#efefef"; 
  86.     $alternate = "1"; 
  87.     } 
  88.  
  89. echo "<tr valign=top bgcolor=$color><td>$id</td><td>$clienttitle</td><td>$dateshow</td><td>$total</td><td>$status</td>";
  90. if ($status == 'pending') {
  91. echo "<td>[ <a href='admin_invoice.php?id=$id'>view / change status</a> ]</td>";
  92. }
  93. else {
  94. echo "<td>[ <a href='admin_invoice.php?id=$id'>view</a> ]</td>";
  95. }
  96. echo "<td>[ <a href='notifyclient.php?id=$id'>notify client</a> ]</td><td>[ <a href='edit_invoice.php?id=$id'>edit</a> ]</td><td>[ <a href='delete_invoice.php?id=$id' onClick=\"return confirm('Are you sure?')\">delete</a> ]</td></tr>";
  97. }
  98. echo "</table>";
  99.  
  100. echo "<p><a href='edit_invoice.php'>add an invoice</a> | <a href='clients.php'>manage client profiles</a>";
  101.  
  102.  
  103.  
  104. }
  105. ?>
  106.  
  107. <p><a href="logout.php">Logout</a></p>
  108.  
  109. <?
  110. include "inc/footer.inc";
  111. ?>
  112.  
  113. </body>
  114.  </html>
I can't seem to log into the "admin" area. I know that this issue has to do with the session because in the second script where we have:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. if ($client_name !== 'admin')
  3. {
  4. ?>
  5. <h2>Hello <b> 
  6.   <? echo $client_name ?>
  7.   </b> </h2>
  8.   Here are your invoices:
  9.  
  10. <?
and after I log in under the admin account, I am not seeing the word "admin" echoed. Does something jump out at anyone? Thanks for looking. Frank
Reply
  #2  
Old May 22nd, 2007, 04:23 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo, Sri Lanka
Posts: 1,421
Default

Please Refer to this link you may find out some solution.
http://au3.php.net/session_register
Reply
  #3  
Old May 22nd, 2007, 04:32 AM
fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
Default

Its Greek to me... Thanks anyway

EDIT: Actually.. I turned register_globals on and the script worked. What do I need to change in the script to allow the script to function while turning register_globals back off? Thanks again
Reply
  #4  
Old May 22nd, 2007, 04:42 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo, Sri Lanka
Posts: 1,421
Default

If i need to use session in my application I'll make it like this.

[PHP]<?php
//First_page.php
session_start();
$TO_SESS_CLIENT = 'AJAXRAND';
$_SESSION['CLIENT_NAME'] = $TO_SESS_CLIENT;
?>
<a href="2.php">GO</a>[/PHP]

[PHP]<?php
//Second_page.php
session_start();
$FROM_SESS_CLIENT= $_SESSION['CLIENT_NAME'];
if($FROM_SESS_CLIENT == 'AJAXRAND'){
echo 'Hi, '.$FROM_SESS_CLIENT;
}
?>[/PHP]
Reply
  #5  
Old May 22nd, 2007, 04:45 AM
fjm fjm is offline
Needs Regular Fix
 
Join Date: May 2007
Location: California
Posts: 348
Default

Ajax, thanks a million man.. I think I am seeing that session_register() is depreciated.. Instead, php.net advises to use a global $_session. I am also seeing that in your example as well. Am I correct on this?

I have changed the session_register to $_session["var"]; but I am getting "undefined variable" errors. Here is what that part of the code I made changes to looks like:

Expand|Select|Wrap|Line Numbers
  1. if (mysql_num_rows($result) == 1)
  2.     {
  3.     session_start();
  4.     $_session["client_id"];
  5.     $_session["client_name"];
  6.     $_session["client_email"];
  7.     $_session["client_ref"];
  8.     $_session["client_title"];
  9.     list($clientid, $name, $pass, $email, $ref, $title) = mysql_fetch_row($result);
  10.     $client_id = $clientid;
  11.     $client_name = $name;
  12.     $client_email = $email;
  13.     $client_ref = $ref;
  14.     $client_title = $title;
  15.     header("Location: menu.php");
  16.  
  17.     mysql_free_result ($result);    
  18.     mysql_close($connection);
  19.     }
Thanks again
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.