Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z sessions
Forum PHP.pl > Forum > PHP
kubatur0
Mam problem. Po kliknieciu przyciusku rejestruj w moim formularzu, bez wypelnienia pol obok powinny pojawic sie bledy ze niewypelnione pola. Formularz jest przesylany do process.php. Zamiast tego pojawia mi sie komunikat: " Fatal error: Cannot redeclare class MySQLDB in C:\xampp\htdocs\Strona\include\database.php on line 14" No ale dobra (w pliku glownym index.php mam include session.php). Z pliku process.php skasowalem tego samego includa bo wlasnie byl zdublowany... ale zamiast tego teraz pojawia sie komunikat: "
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Strona\index.php:19) in C:\xampp\htdocs\Strona\process.php on line 111". Czy ktos moze mi pomoc z tym problemem?


Plik process.php
  1. <?php
  2.  
  3.  
  4.  
  5. class Process
  6. {
  7. /* Class constructor */
  8. function Process(){
  9. global $session;
  10. /* User submitted login form */
  11. if(isset($_POST['sublogin'])){
  12. $this->procLogin();
  13. }
  14. /* User submitted registration form */
  15. else if(isset($_POST['subjoin'])){
  16. $this->procRegister();
  17. }
  18. /* User submitted forgot password form */
  19. else if(isset($_POST['subforgot'])){
  20. $this->procForgotPass();
  21. }
  22. /* User submitted edit account form */
  23. else if(isset($_POST['subedit'])){
  24. $this->procEditAccount();
  25. }
  26. /**
  27. * The only other reason user should be directed here
  28. * is if he wants to logout, which means user is
  29. * logged in currently.
  30. */
  31. else if($session->logged_in){
  32. $this->procLogout();
  33. }
  34. /**
  35. * Should not get here, which means user is viewing this page
  36. * by mistake and therefore is redirected.
  37. */
  38. else{
  39. header("Location: main.php");
  40. }
  41. }
  42.  
  43. /**
  44. * procLogin - Processes the user submitted login form, if errors
  45. * are found, the user is redirected to correct the information,
  46. * if not, the user is effectively logged in to the system.
  47. */
  48. function procLogin(){
  49. global $session, $form;
  50. /* Login attempt */
  51. $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
  52.  
  53. /* Login successful */
  54. if($retval){
  55. header("Location: ".$session->referrer);
  56. }
  57. /* Login failed */
  58. else{
  59. $_SESSION['value_array'] = $_POST;
  60. $_SESSION['error_array'] = $form->getErrorArray();
  61. header("Location: ".$session->referrer);
  62. }
  63. }
  64.  
  65. /**
  66. * procLogout - Simply attempts to log the user out of the system
  67. * given that there is no logout form to process.
  68. */
  69. function procLogout(){
  70. global $session;
  71. $retval = $session->logout();
  72. header("Location: main.php");
  73. }
  74.  
  75. /**
  76. * procRegister - Processes the user submitted registration form,
  77. * if errors are found, the user is redirected to correct the
  78. * information, if not, the user is effectively registered with
  79. * the system and an email is (optionally) sent to the newly
  80. * created user.
  81. */
  82. function procRegister(){
  83. global $session, $form;
  84. /* Convert username to all lowercase (by option) */
  85. if(ALL_LOWERCASE){
  86. $_POST['user'] = strtolower($_POST['user']);
  87. }
  88. /* Registration attempt */
  89. $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']);
  90.  
  91. /* Registration Successful */
  92. if($retval == 0){
  93. $_SESSION['reguname'] = $_POST['user'];
  94. $_SESSION['regsuccess'] = true;
  95. header("Location: ".$session->referrer);
  96. }
  97. /* Error found with form */
  98. else if($retval == 1){
  99. $_SESSION['value_array'] = $_POST;
  100. $_SESSION['error_array'] = $form->getErrorArray();
  101. header("Location: ".$session->referrer);
  102. }
  103. /* Registration attempt failed */
  104. else if($retval == 2){
  105. $_SESSION['reguname'] = $_POST['user'];
  106. $_SESSION['regsuccess'] = false;
  107. header("Location: ".$session->referrer);
  108. }
  109. }
  110.  
  111. /**
  112. * procForgotPass - Validates the given username then if
  113. * everything is fine, a new password is generated and
  114. * emailed to the address the user gave on sign up.
  115. */
  116. function procForgotPass(){
  117. global $database, $session, $mailer, $form;
  118. /* Username error checking */
  119. $subuser = $_POST['user'];
  120. $field = "user"; //Use field name for username
  121. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  122. $form->setError($field, "* Username not entered<br>");
  123. }
  124. else{
  125. /* Make sure username is in database */
  126. $subuser = stripslashes($subuser);
  127. if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
  128. !eregi("^([0-9a-z])+$", $subuser) ||
  129. (!$database->usernameTaken($subuser))){
  130. $form->setError($field, "* Username does not exist<br>");
  131. }
  132. }
  133.  
  134. /* Errors exist, have user correct them */
  135. if($form->num_errors > 0){
  136. $_SESSION['value_array'] = $_POST;
  137. $_SESSION['error_array'] = $form->getErrorArray();
  138. }
  139. /* Generate new password and email it to user */
  140. else{
  141. /* Generate new password */
  142. $newpass = $session->generateRandStr(8);
  143.  
  144. /* Get email of user */
  145. $usrinf = $database->getUserInfo($subuser);
  146. $email = $usrinf['email'];
  147.  
  148. /* Attempt to send the email with new password */
  149. if($mailer->sendNewPass($subuser,$email,$newpass)){
  150. /* Email sent, update database */
  151. $database->updateUserField($subuser, "password", md5($newpass));
  152. $_SESSION['forgotpass'] = true;
  153. }
  154. /* Email failure, do not change password */
  155. else{
  156. $_SESSION['forgotpass'] = false;
  157. }
  158. }
  159.  
  160. header("Location: ".$session->referrer);
  161. }
  162.  
  163. /**
  164. * procEditAccount - Attempts to edit the user's account
  165. * information, including the password, which must be verified
  166. * before a change is made.
  167. */
  168. function procEditAccount(){
  169. global $session, $form;
  170. /* Account edit attempt */
  171. $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']);
  172.  
  173. /* Account edit successful */
  174. if($retval){
  175. $_SESSION['useredit'] = true;
  176. header("Location: ".$session->referrer);
  177. }
  178. /* Error found with form */
  179. else{
  180. $_SESSION['value_array'] = $_POST;
  181. $_SESSION['error_array'] = $form->getErrorArray();
  182. header("Location: ".$session->referrer);
  183. }
  184. }
  185. };
  186.  
  187. /* Initialize process */
  188. $process = new Process;
  189.  
  190. ?>


czesc pliku index.php

  1. <?php
  2. include("/include/session.php");
  3. ?>
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  6. <html>
  7. <head>
  8.  
  9. <link rel="stylesheet" type="text/css" href="css/style.css" />
  10. </head>
  11. <body>
  12. <div id="top">
  13. <div id="NAGLOWEK"></div>
  14. <div id="MENUGORNE">
  15. <?php
  16. include("/components/menugorne.html");
  17. ?>
  18. </div>
  19. <div id="NAW1"></div>
  20. <div id="MENU">
  21. <?php
  22.  
  23. .
  24. .
  25. .


Z gory dzieki za pomoc
Spawnm
Używaj poprawnego bbcode dla php,
Błędy dokładnie opisują co robisz źle.
A o headers already sent było milion razy , poszukaj.
kubatur0
No wiem szukalem , wszystkie biale znaki przed <?php i za ?> kasowalem i nicnie pomagalo.
dodalem na poczatku ob_start(); i na koncu ob_end_flush(); i tez nic sie nie zmienilo...
Smertius
Użyj kodowania UTF-8 bez bom np. notepad++. Pokaż plik session.php może tam jest coś co wysyła jakieś informacje do przeglądarki zanim rozpoczniesz sesję
kubatur0
  1. <?php
  2. include("database.php");
  3. include("mailer.php");
  4. include("form.php");
  5.  
  6. (...)
  7.  
  8. function Session(){
  9. $this->time = time();
  10. $this->startSession();
  11. }
  12.  
  13. function startSession(){
  14. global $database; //The database connection
  15. session_start(); //Tell PHP to start the session
  16.  
  17. $this->logged_in = $this->checkLogin();
  18.  
  19. (...)
  20.  
  21. if(isset($_SESSION['url'])){
  22. $this->referrer = $_SESSION['url'];
  23. }else{
  24. $this->referrer = "/";
  25. }
  26.  
  27. $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  28. }
  29.  
  30. function checkLogin(){
  31. global $database; //The database connection
  32.  
  33. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  34. $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
  35. $this->userid = $_SESSION['userid'] = $_COOKIE['cookid'];
  36. }
  37.  
  38. if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
  39. $_SESSION['username'] != GUEST_NAME){
  40.  
  41. if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
  42.  
  43. unset($_SESSION['username']);
  44. unset($_SESSION['userid']);
  45. return false;
  46. }
  47.  
  48. $this->userinfo = $database->getUserInfo($_SESSION['username']);
  49. $this->username = $this->userinfo['username'];
  50. $this->userid = $this->userinfo['userid'];
  51. $this->userlevel = $this->userinfo['userlevel'];
  52. return true;
  53. }
  54.  
  55. else{
  56. return false;
  57. }
  58. }
  59.  
  60. function login($subuser, $subpass, $subremember){
  61. global $database, $form; //The database and form object
  62.  
  63. $field = "user"; //Use field name for username
  64. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  65. $form->setError($field, "* Username not entered");
  66. }
  67. else{
  68.  
  69. if(!eregi("^([0-9a-z])*$", $subuser)){
  70. $form->setError($field, "* Username not alphanumeric");
  71. }
  72. }
  73.  
  74. $field = "pass"; //Use field name for password
  75. if(!$subpass){
  76. $form->setError($field, "* Password not entered");
  77. }
  78.  
  79. if($form->num_errors > 0){
  80. return false;
  81. }
  82.  
  83. $subuser = stripslashes($subuser);
  84. $result = $database->confirmUserPass($subuser, md5($subpass));
  85.  
  86. if($result == 1){
  87. $field = "user";
  88. $form->setError($field, "* Username not found");
  89. }
  90. else if($result == 2){
  91. $field = "pass";
  92. $form->setError($field, "* Invalid password");
  93. }
  94.  
  95. if($form->num_errors > 0){
  96. return false;
  97. }
  98.  
  99. $this->userinfo = $database->getUserInfo($subuser);
  100. $this->username = $_SESSION['username'] = $this->userinfo['username'];
  101. $this->userid = $_SESSION['userid'] = $this->generateRandID();
  102. $this->userlevel = $this->userinfo['userlevel'];
  103.  
  104. $database->updateUserField($this->username, "userid", $this->userid);
  105. $database->addActiveUser($this->username, $this->time);
  106. $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);
  107.  
  108. if($subremember){
  109. setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
  110. setcookie("cookid", $this->userid, time()+COOKIE_EXPIRE, COOKIE_PATH);
  111. }
  112.  
  113. return true;
  114. }
  115.  
  116. function logout(){
  117. global $database; //The database connection
  118.  
  119. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  120. setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  121. setcookie("cookid", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  122. }
  123.  
  124. unset($_SESSION['username']);
  125. unset($_SESSION['userid']);
  126.  
  127. $this->logged_in = false;
  128.  
  129. $database->removeActiveUser($this->username);
  130. $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  131.  
  132. $this->username = GUEST_NAME;
  133. $this->userlevel = GUEST_LEVEL;
  134. }
  135. function register($subuser, $subpass, $subemail){
  136. global $database, $form, $mailer; //The database, form and mailer object
  137.  
  138. $field = "user"; //Use field name for username
  139. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  140. $form->setError($field, "* Username not entered");
  141. }
  142. else{
  143.  
  144. $subuser = stripslashes($subuser);
  145. if(strlen($subuser) < 5){
  146. $form->setError($field, "* Username below 5 characters");
  147. }
  148. else if(strlen($subuser) > 30){
  149. $form->setError($field, "* Username above 30 characters");
  150. }
  151.  
  152. else if(!eregi("^([0-9a-z])+$", $subuser)){
  153. $form->setError($field, "* Username not alphanumeric");
  154. }
  155.  
  156. else if(strcasecmp($subuser, GUEST_NAME) == 0){
  157. $form->setError($field, "* Username reserved word");
  158. }
  159.  
  160. else if($database->usernameTaken($subuser)){
  161. $form->setError($field, "* Username already in use");
  162. }
  163.  
  164. else if($database->usernameBanned($subuser)){
  165. $form->setError($field, "* Username banned");
  166. }
  167. }
  168. $field = "pass"; //Use field name for password
  169. if(!$subpass){
  170. $form->setError($field, "* Password not entered");
  171. }
  172. else{
  173.  
  174. $subpass = stripslashes($subpass);
  175. if(strlen($subpass) < 4){
  176. $form->setError($field, "* Password too short");
  177. }
  178.  
  179. else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
  180. $form->setError($field, "* Password not alphanumeric");
  181. }
  182.  
  183. }
  184. $field = "email"; //Use field name for email
  185. if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  186. $form->setError($field, "* Email not entered");
  187. }
  188. else{
  189.  
  190. $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  191. ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  192. ."\.([a-z]{2,}){1}$";
  193. if(!eregi($regex,$subemail)){
  194. $form->setError($field, "* Email invalid");
  195. }
  196. $subemail = stripslashes($subemail);
  197. }
  198.  
  199. if($form->num_errors > 0){
  200. return 1; //Errors with form
  201. }
  202.  
  203. else{
  204. if($database->addNewUser($subuser, md5($subpass), $subemail)){
  205. if(EMAIL_WELCOME){
  206. $mailer->sendWelcome($subuser,$subemail,$subpass);
  207. }
  208. return 0; //New user added succesfully
  209. }else{
  210. return 2; //Registration attempt failed
  211. }
  212. }
  213. }
  214.  
  215. function editAccount($subcurpass, $subnewpass, $subemail){
  216. global $database, $form; //The database and form object
  217.  
  218. if($subnewpass){
  219.  
  220. $field = "curpass"; //Use field name for current password
  221. if(!$subcurpass){
  222. $form->setError($field, "* Current Password not entered");
  223. }
  224. else{
  225.  
  226. $subcurpass = stripslashes($subcurpass);
  227. if(strlen($subcurpass) < 4 ||
  228. !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
  229. $form->setError($field, "* Current Password incorrect");
  230. }
  231.  
  232. if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
  233. $form->setError($field, "* Current Password incorrect");
  234. }
  235. }
  236.  
  237. $field = "newpass"; //Use field name for new password
  238.  
  239. $subpass = stripslashes($subnewpass);
  240. if(strlen($subnewpass) < 4){
  241. $form->setError($field, "* New Password too short");
  242. }
  243.  
  244. else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
  245. $form->setError($field, "* New Password not alphanumeric");
  246. }
  247. }
  248.  
  249. else if($subcurpass){
  250. $field = "newpass"; //Use field name for new password
  251. $form->setError($field, "* New Password not entered");
  252. }
  253.  
  254. $field = "email"; //Use field name for email
  255. if($subemail && strlen($subemail = trim($subemail)) > 0){
  256. /* Check if valid email address */
  257. $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  258. ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  259. ."\.([a-z]{2,}){1}$";
  260. if(!eregi($regex,$subemail)){
  261. $form->setError($field, "* Email invalid");
  262. }
  263. $subemail = stripslashes($subemail);
  264. }
  265.  
  266. if($form->num_errors > 0){
  267. return false; //Errors with form
  268. }
  269.  
  270. if($subcurpass && $subnewpass){
  271. $database->updateUserField($this->username,"password",md5($subnewpass));
  272. }
  273. if($subemail){
  274. $database->updateUserField($this->username,"email",$subemail);
  275. }
  276.  
  277. return true;
  278. }
  279. function isAdmin(){
  280. return ($this->userlevel == ADMIN_LEVEL ||
  281. $this->username == ADMIN_NAME);
  282. }
  283.  
  284. function generateRandID(){
  285. return md5($this->generateRandStr(16));
  286. }
  287.  
  288. function generateRandStr($length){
  289. $randstr = "";
  290. for($i=0; $i<$length; $i++){
  291. $randnum = mt_rand(0,61);
  292. if($randnum < 10){
  293. $randstr .= chr($randnum+48);
  294. }else if($randnum < 36){
  295. $randstr .= chr($randnum+55);
  296. }else{
  297. $randstr .= chr($randnum+61);
  298. }
  299. }
  300. return $randstr;
  301. }
  302. };
  303. $session = new Session;
  304. $form = new Form;
  305. ?>
To jest moj plik sessions.php. Mam ustawione kodowanie utf-8 bez BOM. Musialem skrocic troche ten plik na poczatku przez ograniczenia wielkosci posta...
Smertius
Daj kod w poprawne tagi dla php bo tego sie czytac nie da
kubatur0
W pliku glownym index.php dodalem ob_start(); i session_start(); i na koncu ob_flush();

Teraz nie pokazuje mi sie juz zaden taki blad, ale za to teraz znowu jest ten sam blad ktory byl na poczatku, a mianowicie jak w formularzu rejestracyjnym nie wypelni sie odpowiednich pol to powinno obok nich pojawiac sie error ze nie wypelnione, a zamiast tego przekierowuje mi na strone glowna gdzie jest panel logowania i nad panelem pokazuje sie blad logowania (tak jakby sie podczas logowania cos rypalo a nie rejestracji).
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.