By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

PHP sessions

P: 13
i am using php 5.2.5. i have a php script like this:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7. <body>
  8. <?php
  9. ob_start();
  10. $host="localhost"; // Host name
  11. $username="root"; // Mysql username
  12. $password="dhanshi"; // Mysql password
  13. $db_name="adi"; // Database name
  14. $tbl_name="users"; // Table name
  15.  
  16. // Connect to server and select databse.
  17. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  18. mysql_select_db("$db_name")or die("cannot select DB");
  19.  
  20. // Define $myusername and $mypassword
  21. $myusername=$_POST['name'];
  22. $mypassword=$_POST['pass'];
  23.  
  24. // To protect MySQL injection 
  25. $myusername = stripslashes($myusername);
  26. $mypassword = stripslashes($mypassword);
  27. $myusername = mysql_real_escape_string($myusername);
  28. $mypassword = mysql_real_escape_string($mypassword);
  29.  
  30. $sql="SELECT * FROM $tbl_name WHERE name='$myusername' and pass=md5('$mypassword')";
  31. $result=mysql_query($sql);
  32.  
  33. // Mysql_num_row is counting table row
  34. $count=mysql_num_rows($result);
  35. // If result matched $myusername and $mypassword, table row must be 1 row
  36. $r=mysql_fetch_array($result);
  37.  
  38. $value=$r['value'];
  39.  
  40.  if($count==1){
  41.  
  42.   session_register("myusername");
  43. session_register("mypassword");
  44.  
  45. //echo $value;
  46. //echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login_success.php">';  
  47. switch($value)
  48. {
  49. case "1";
  50. case "2";
  51.  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=tl.php">';  
  52.  break;
  53.  case "3";
  54.  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=l2.php">';  
  55.  break;
  56.  case "4";
  57. echo '<META HTTP-EQUIV="Refresh" Content="0; URL=l3.php">'; 
  58.  
  59.  default:
  60. echo '<META HTTP-EQUIV="Refresh" Content="0; URL=l3.php">'; 
  61.  
  62.  exit;
  63. }
  64.  
  65.  
  66. }
  67. else {
  68. echo '<META HTTP-EQUIV="Refresh" Content="0; URL=wrong.php">';  
  69. }
  70. ob_end_flush();
  71. ?>
when the script runs, i get a warning:


Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\adi\check.php:8) in C:\xampp\htdocs\adi\check.php on line 42.

Line 8 and 42 is highlighted here.

Can this problem be solved?
Aug 30 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hi.

You need to call session_start() before anything is added to the output. It should be the first thing on the page.

The session functions, like session_register, are deprecated and shouldn't be used.
I suggest you take a look at my PHP Session article to see how sessions should be used in PHP5.
Aug 30 '08 #2

P: 13
Hi.

You need to call session_start() before anything is added to the output. It should be the first thing on the page.

The session functions, like session_register, are deprecated and shouldn't be used.
I suggest you take a look at my PHP Session article to see how sessions should be used in PHP5.
HI Atli,

Ur suggestion works. Thanks a lot.
Aug 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.