SunQuest
           Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old March 2nd, 2004, 11:52 PM
SnowStorm SnowStorm is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 374 SnowStorm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Sessions tutorial?

I can't get the sessions tutorial for the login part to work. I dont understand how it works or how it is asking me to select the values at the login_check part of the functions page, could someone help me?

Here is code I am trying to use
php Code:
Original - php Code
  1.  
  2. (functions.php)
  3. <?php
  4. //database settings
  5. $hostName="";
  6. $userName="";
  7. $passWord="";
  8. $dataBase="";
  9.  
  10. //connect to databse
  11. $conn = mysql_connect($hostName,$userName,$passWord) or die("Failed to connect to database!");
  12. mysql_select_db($dataBase,$conn) or die("Failed to select database!");
  13. function secure ()
  14. {
  15.   if(!($_SESSION["id"]) || ($_SESSION["id"] == ""))
  16.   {
  17.     Header("Location: ./login.php");
  18.     exit();
  19.   }
  20. }
  21. function login_check ($forms)
  22. {
  23.   $error = "";
  24.   $username = $forms["username"];
  25.   $password = $forms["password"];
  26.   if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
  27.   if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
  28.   $sql = "SELECT Username,Password FROM userLogin WHERE Username='$username' AND Password='$password'";
  29.   $result = mysql_query($sql) or die(mysql_error());
  30.   $num = mysql_num_rows($result);
  31.   if($num)
  32.   {
  33.     return TRUE;
  34.   }
  35.   else
  36.   {
  37.     return FALSE;
  38.   }
  39. }
  40.  
  41. function login ($forms)
  42. {
  43.   $username = $forms["username"];
  44.   $password = $forms["password"];
  45.   $sql = "SELECT ID FROM userLogin WHERE Username='$username' AND Password='$password'";
  46.   $result = mysql_query($sql) or die(mysql_error());
  47.   $id = mysql_fetch_assoc($result);
  48.   return $id;
  49. }
  50.  
  51. ?>
  52.  
  53. (login.php)
  54. <?php
  55. // login.php
  56. include ("functions.php");
  57. if($_POST)
  58. {
  59.   $error = login_check($_POST);
  60.   if (trim($error)=="")
  61.   {
  62.     $_SESSION["id"] = login($_POST);
  63.     Header("Location: ./index.php"); // Redirect correct member
  64.     exit();
  65.   }
  66.   else
  67.   {
  68.     print "Error:$error";
  69.   }
  70. }
  71.  
  72. print("<form method="post" action="{$_SERVER['PHP_SELF']}">
  73. Username : <input type="text" name="username"><br>
  74. Password : <input type="password" name="password"><br>
  75. <input type="submit" value="Login">
  76. </form>");
  77. ?>


Here is the tutorial link:
http://codewalkers.com/tutorials/32/3.html

Btw it just says Error: 1 when I try this.

Reply With Quote
  #2  
Old March 3rd, 2004, 12:29 AM
System System is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Posts: 665 System User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Message Moved

Thread moved from 'PHP Coding' to 'Tutorials' by sliver.

Reason: Help with tutorial code

Reply With Quote
  #3  
Old March 3rd, 2004, 02:34 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Sessions tutorial?

Check these two scripts in your pages.. did you create the table in database and make sure about the MySQL connection.

(login.php)
php Code:
Original - php Code
  1. <?php
  2. // login.php
  3. include ("functions.php");
  4. if($_POST['submit'])
  5. {
  6.     $username=$_POST['username'];
  7.     $password=$_POST['password'];
  8.   $error = login_check($username,$password);
  9.   if($error==true)
  10.   {
  11.     $_SESSION['id'] = login($username,$password);
  12.     header("Location: index.php");
  13.   }
  14.   else
  15.     print "Error:$error";
  16. }
  17. ?>
  18. <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
  19. Username : <input type="text" name="username"><br>
  20. Password : <input type="password" name="password"><br>
  21. <input type="submit" name="submit" value="Login">
  22. </form>


(functions.php)
php Code:
Original - php Code
  1.  
  2. <?php
  3. $hostName="";
  4. $userName="";
  5. $passWord="";
  6. $dataBase="";
  7.  
  8. $conn = mysql_connect($hostName,$userName,$passWord) or die("Failed to connect to database!");
  9. mysql_select_db($dataBase,$conn) or die("Failed to select database!");
  10.  
  11. function secure ()
  12. {
  13.   if(!($_SESSION["id"]) || ($_SESSION["id"] == ""))
  14.     header("Location: login.php");
  15. }
  16.  
  17. function login_check($username,$password)
  18. {
  19.   if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
  20.   if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
  21.   $sql = "SELECT Username,Password FROM userLogin WHERE Username='$username' AND Password='$password'";
  22.   $result = mysql_query($sql) or die(mysql_error());
  23.   $num = mysql_num_rows($result);
  24.   if($num)
  25.       return TRUE
  26.   else
  27.     $error .="<li>Invalid username/password.</li>";
  28.   return $error;
  29. }
  30.  
  31. function login($username,$password)
  32. {
  33.   $sql = "SELECT ID FROM userLogin WHERE Username='$username' AND Password='$password'";
  34.   $result = mysql_query($sql) or die(mysql_error());
  35.   $row= mysql_fetch_array($result);
  36.   $id = $row['ID'];
  37.   return $id;
  38. }
  39. ?>

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Sessions tutorial?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |