Jump to content


Photo

functions and sessions


  • Please log in to reply
4 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 03:24 AM

I am starting to put alot of my backend processing into functions. So now I have a login script that I am wondering if I should start the session in the login script. Here we go.

Here is a section of the process.php

function showProcess($process) {
    switch ($process) {
	   case "login":
                    include("functions.php");
	        array_pop($_POST);
	        if (get_magic_quotes_gpc()) {
	        $_POST= array_map('stripslashes', $_POST);
	        }
	        $username = mysql_real_escape_string(trim($_POST['username']));
	        $password = mysql_real_escape_string(trim($_POST['password']));
	        $mdpwd = md5($password);
		  
	        $login = checkUser();
		  
	        if ($login == "true") {
	            echo "You are now loged in";
	        }else{
	            echo "$login";
	        }
	   break;


and here is the checkUser function

function checkUser() {
   include("db.php");
   global $username;
   global $mdpwd;
   
   $sql = sprintf("SELECT COUNT(*) AS `login_match` FROM `users` WHERE `username` = '%s' AND `password` ='%s'", $username, $mdpwd);
   $res = mysql_query($sql) or die(mysql_error());
   $login_match = mysql_result($res, 0, 'login_match');
   
   if ($login_match == 1) {
       $q = mysql_query("SELECT * FROM `users` WHERE `username` ='$username'") or die(mysql_error());
	   
       while ($rw = mysql_fetch_assoc($q)) {
            // I will set my session variables
            $_SESSION['whatever'] = $rw['whatever'];
        }
        $result = "true";
   }else{
        $result = "Your username and password do not match";
   }
   return $result;
}


My question is this a good way of doing this or ? and where should I place the session start? Should it be in the function or at the begining of the functions.php. 

Thanks,
Tom              

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 August 2006 - 03:42 AM

Begining of the file is where i put it... then again any file ive ever created using sessions starts like
<?
session_start();
Why doesn't anyone ever say hi, hey, or whad up world?

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 03:46 AM

I know that but which one should I place the session_start in. Should it be the functions.php or the process.php or both. Does the session carry over into the function?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 07 August 2006 - 03:51 AM

are you including function.php?

'Cause if you are just make sure session_start is above the include command in process.php
Why doesn't anyone ever say hi, hey, or whad up world?

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 07 August 2006 - 04:05 AM

I guess I should not be lazy and test these things prior to posting..lol

Thanks for the help

Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users