Trium918 Posted June 6, 2007 Share Posted June 6, 2007 Could someone please explain to me why the member session will not register? <?php session_start(); $query = "SELECT * FROM members_info WHERE user='$user' AND passwd=MD5('$passwd') LIMIT 1"; $result = mysql_query($query); $row=mysql_fetch_assoc($result); $memberid = $row['id']; $_SESSION['memberid'] = $memberid ; /*print_r($_SESSION['memberid']); exit;*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/ Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 echo 'Member ID = ' . $memberid; print_r($_SESSION); exit; Try it that way. Make sure the memberid is valid. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269493 Share on other sites More sharing options...
Trium918 Posted June 6, 2007 Author Share Posted June 6, 2007 echo 'Member ID = ' . $memberid; print_r($_SESSION); exit; Try it that way. Make sure the memberid is valid. I donnot understand! Your code and the code that I provided does the same thing, and the result is equal to 1. Which is the user id, but the session isn't registering. <?php print_r($_SESSION['memberid']); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269506 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Kinda, mine prints out the whole session variable. Why not do a further test like this: page1.php <?php session_start(); if (!isset($_SESSION['memberid'])) { $_SESSION['memberid'] = 1; echo '<a href="page1.php">Click Here</a>'; }else { echo 'Yay the session went through! Phew! memberid = ' . $_SESSION['memberid']; } ?> See if that works. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269511 Share on other sites More sharing options...
Trium918 Posted June 6, 2007 Author Share Posted June 6, 2007 Your code works just fine, but what code be going wrong with my script. Note: session_start is at the top of each page. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269522 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 I have no clue. Where is the page you are trying to read memberid that is not working? Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269524 Share on other sites More sharing options...
Trium918 Posted June 6, 2007 Author Share Posted June 6, 2007 I have no clue. Where is the page you are trying to read memberid that is not working? You never responsed to my other topic so I decided to just rewrite my log in script. <?php function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; $result = mysql_query($query); $row=mysql_fetch_assoc($result); /*print_r($_SESSION['memberid']); exit;*/ $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; /$last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); if (!$result){ return false; } if (mysql_num_rows($result)>0){ return true; } else { return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269538 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 With what you just posted, there is no session_start() at the top. I guess I do not know what the other topic was, sorry about that. Anyhow does the new script work? If not show where you are trying to access the memberid if it is in another script, if not and you are just trying to access it further down the line, I am not sure if sessions are like cookies or not. I know with cookies you cannot access that data until the page is refreshed/reloaded... Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269542 Share on other sites More sharing options...
Trium918 Posted June 6, 2007 Author Share Posted June 6, 2007 Ok, this is how my script works. First the user is prompted to log in from login.php. The form is sent to member.php, and the session is started at the top of that page. There is another file called user_auth_fns.php and the login script is store there. <?php session_start(); // connect to database // retrieve default image $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['membersid']}'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $default_image = $row['default_image']; echo "<img src=\"$default_image\" height=\"120\" width=\"120\" alt=\" \" />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269550 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 After I showed you all that error handling (in the last script your posted), looking at your other code, you've gone back to your own lazy ways. Error handling is the key to a reliable program. Basically, anything that could be wrapped in an if() should be. How do you know your query actually works and that $memberid has any value Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269695 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 After I showed you all that error handling (in the last script your posted), looking at your other code, you've gone back to your own lazy ways. Error handling is the key to a reliable program. Basically, anything that could be wrapped in an if() should be. How do you know your query actually works and that $memberid has any value Which post? I am using the technique you showed me in the script below. <?php session_start(); // connect to database // retrieve default image $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['membersid']}'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $default_image = $row['default_image']; echo "<img src=\"$default_image\" height=\"120\" width=\"120\" alt=\" \" />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> thorpe script <?php // http://www.phpfreaks.com/forums/index.php/topic,142758.msg609485.html#msg609485 $sql = "SELECT foo FROM tbl"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['foo']."<br />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269711 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 Yeah... but look at your first and third post in this thread. Probably not your actual issue, but poor coding. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-269760 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Yeah... but look at your first and third post in this thread. Probably not your actual issue, but poor coding. I tried this method. The memberid session isn't registering. The last visit session isn't as well. <?php function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row=mysql_fetch_assoc($result); /*print_r($_SESSION['memberid']); exit;*/ $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; } }else { echo "Query failed<br />$query<br />" . mysql_error(); } if (!$result){ return false; } if (mysql_num_rows($result)>0){ return true; } else { return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270037 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Are you using localhost? localhost problem ----------------- When using localhost and session.cookies not work, simply remove domain from session.cookie_domain. We can use "session_set_cookie_params" function or ini_set to do this. Example: $cookie_path = 'my_path'; $cookie_domain = 'localhost'; //or any valid domain init_session_cookies($cookie_path, $cookie_domain); //maybe we create new function for handling this function init_session_cookies($path, $domain) { if ($domain=='localhost') $domain=''; if (function_exists('session_set_cookie_params')) { session_set_cookie_params(0, $path, $domain); } else { ini_set('session.cookie_lifetime', '0'); ini_set('session.cookie_path', $path); ini_set('session.cookie_domain', $domain); } } Best, Parikesit (zae_lokamaya #at# yahoo #dot# co #dot# uk) Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270040 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Thanks frost110, but the problem is that I am not writing the code correctly. I'll tried to rewrite the script outside of the function. I think because the script is with in the function it is limited. What do you all think? Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270048 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Really with session variables they are global, having it inside the function should not limit it. You can do a simple test once again to test: <?php session_start(); function set_session() { $_SESSION['mytest'] = "This is only a test"; return; } if (!isset($_SESSION['mytest'])) { set_session(); echo '<a href="page1.php">Click Here</a>'; }else { echo 'Yay the session went through! Phew! mytest = ' . $_SESSION['mytest']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270061 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Why won't the session register? I am asking for someone to point out the problem! I have attached 4 files below. There are not long scripts. The is user is prompted to log in from login.php. The form is submitted to member.php. There is call to a function called login($user_name,$password) which is defined in user_auth.php. The other two files are the database file and container_fns.php. The structure of how the database is set up is in the database file. Wasn't able to attach user_auth.php <?php // I can include this file in all of my files // this way, every file will contain all of the functions require_once("db_fns.php"); require_once("user_auth.php"); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270223 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Ok first off that is the first time I saw those files. If you would of posted them earlier this would of been solved without wasting our time. // not protected for SQL injection $user = trim($_POST['user']); $pass = trim($_POST['pass']); // function declared inside of user_auth.php $result_login = login($user_name, $password); That is where your main problem is. First off you form references username, not user, which should be your post index. Second off you assign to $user not $user_name. Third off in your login call you are calling $user_name instead of $user or $_POST['username']. Finally the reason you did not know this was happening is because of the login function and how it handled data: function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { // Note the "greater than 0" $row=mysql_fetch_assoc($result); /*print_r($_SESSION['memberid']); exit;*/ $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; } }else { echo "Query failed<br />$query<br />" . mysql_error(); } } You should probably start paying attention to variable names and how you reference/assign them. Save yourself and everyone else some headache bud. EDIT: Try this login function instead: <?php // database connection file require_once("db_fns.php"); function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row=mysql_fetch_assoc($result); /*print_r($_SESSION['memberid']); exit;*/ $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; return false; } }else { echo "Query failed<br />$query<br />" . mysql_error(); return false; } return true; } ?> As the old function, your failure codes would never be touched as even an empty set is considered a good result. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270228 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Sorry, frost110! I for got to edit the user, pass. You are right, it should I been $user_name and $password. The originally code is correct. I wrote those attachments while at school earler to today. The memberid session isn't showing up still when it is called in member.php for the image. <?php function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row=mysql_fetch_assoc($result); /*print_r($_SESSION['memberid']); exit;*/ $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; return false; } }else { echo "Query failed<br />$query<br />" . mysql_error(); return false; } return true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270234 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['memerid']}'"; Shouldn't that be: $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['memberid']}'"; memberid And shouldn't this (login function) also be memberid $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['memberid']}'"; //now only update it Becareful how you call variable names. If those do not work, please post inline the current login function and the current members.php page. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270237 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['memerid']}'"; Shouldn't that be: $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['memberid']}'"; memberid And shouldn't this (login function) also be memberid $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['memberid']}'"; //now only update it Becareful how you call variable names. Yes, it is correct Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270238 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Well if that did not solve the problem, please post the current/updated members.php and the current login function inline, not as an attachment here. It makes it easier to see the syntax highlighted. The reason I point those out is because they are keys, and if I do not see code with the correct names etc, well it is hard to diagnose when I do not know which version you are using. Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270239 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Please ask questions! member.php <?php session_start(); require_once("container_fns"); if($_POST['submit']){ // validate the user input // would go here. // not protected for SQL injection $user = trim($_POST['user_name']); $pass = trim($_POST['password']); // function declared inside of user_auth.php $result_login = login($user_name, $password); // if the user enter wrong user/password if(!result_login) { echo "Wrong password or user_name"; } // if they are in the database register the user id // and password $valid_user = $user_name; $_SESSION['valid_user']= $valid_user; $password_str = $password; $password_str = md5($password_str); $_SESSION['password'] = $password_str; }// end of submit // connect to database // retrieve default image $sql = "SELECT default_image FROM members_info HERE members_id='{$_SESSION['memberid']}'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $default_image = $row['default_image']; echo "<img src=\"$default_image\" height=\"120\" width=\"120\" alt=\" \" />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> user_auth.php <?php require_once("db_fns.php"); function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row=mysql_fetch_assoc($result); $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid ; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; return false; } }else { echo "Query failed<br />$query<br />" . mysql_error(); return false; } return true; } ?> db.fns.php <?php // connects to the database function db_connect() { $result = mysql_pconnect("localhost"/*, " ", " "*/); if (!$result) return false; if (!mysql_select_db(" ")) return false; return $result; } /* drop table if exists members_info; create table members_info(members_id int unsigned not NULL auto_increment primary key, user_name varchar(25) not NULL, first_name varchar(25) not NULL, last_name varchar(25) not NULL, gender varchar(6) not NULL, birth_month varchar(9) not NULL, birth_day tinyint(2) not NULL, birth_year int(4) not NULL, contact_number varchar(10) not NULL, email_address varchar(100) not NULL, register_date datetime not NULL default'0000-00-00',password varchar(32) not NULL, last_visit datetime not NULL default'0000-00-00',default_image varchar(50) not NULL); */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270245 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Alright, I am sorry but until I see corrected code I am still not sure that it is running like this should. in the member.php if(!result_login) { echo "Wrong password or user_name"; } should be if(!$result_login) { echo "Wrong password or user_name"; } In the login.php $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['membersid']}'"; //now only update it $result2=mysql_query($sql); should still be $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['memberid']}'"; //now only update it $result2=mysql_query($sql); Now I know this may seem redundant, but are you absolutely sure that this script is even entering into the portion where it retrieves the member id? If not setup an echo or a die statement something like this: login.php if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row=mysql_fetch_assoc($result); echo 'We reached it! Here is $row!!<br /><pre>', print_r($row),'</pre>'; die(); And see what is being returned there. If it all looks good than we know that the form is passing the right information to the script and that that portion of the code is working. Next I do not see where the user_auth is being included, is that inside the require_once("container_fns"); if so, why does that not have .php after wards? Just curious. Also could you post the form, just to double check the names etc. Let me know on that debug statement what comes out of it. After looking through members.php I found some other issues, try using this instead: <?php session_start(); require_once("container_fns"); if($_POST['submit']){ // validate the user input // would go here. // not protected for SQL injection $user = trim($_POST['user_name']); $pass = trim($_POST['password']); // function declared inside of user_auth.php $result_login = login($user_name, $password); // if the user enter wrong user/password if(!$result_login) { echo "Wrong password or user_name"; } // if they are in the database register the user id // and password $valid_user = $user_name; $_SESSION['valid_user']= $valid_user; $password_str = $password; $password_str = md5($password_str); $_SESSION['password'] = $password_str; echo "Memberid IS: " . $_SESSION['memberid'] . "<br />"; }// end of submit // connect to database // retrieve default image $sql = "SELECT default_image FROM members_info HERE members_id='{$_SESSION['memberid']}'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { // added greater than zero here. while ($row = mysql_fetch_assoc($result)) { $default_image = $row['default_image']; echo "<img src=\"$default_image\" height=\"120\" width=\"120\" alt=\" \" />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> Also are you sure that there is an image record for the $_SESSION['memberid'] ?? Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270251 Share on other sites More sharing options...
Trium918 Posted June 7, 2007 Author Share Posted June 7, 2007 Why won't the session register? Ok, I will brake the code down. I am asking for someone to point out the problem! Note: Follow the order in which there in. The user is prompted to log in from login.php. The form is submitted to member.php. There is call to a function called login($user_name,$password) which is defined in user_auth.php. The other two files are the database file and container_fns.php. The structure of how the database is set up is in the database file. login.php <form method="POST" action="member.php"> <h1 align="center">Test log in </h1> <table border="1" align="center"> <tr> <td>Username:</td> <td><input type="text" name="user_name" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td> </tr> </table> </form> member.php <?php session_start(); require_once("container_fns.php"); if (isset($_POST['submit'])) #They have just tried logging in { $user_name= trim($_POST['user_name']); $password = trim($_POST['password']); #Checks that username is in database and password is correct $result = login($_POST['user_name'], $_POST['password']); // If user isn't valid if(!$result){ // unsuccessful login echo "<p class='genmed'>You could not be logged in. You must be logged in to view this page. <br /> $user_name $password</p>"; exit; } // if they are in the database register the user id $valid_user = $user_name; $_SESSION['valid_user']= $valid_user; $password_str = $password; $password_str = md5($password_str); $_SESSION['password'] = $password_str; }// end of submit // connect to database // retrieve default image $sql = "SELECT default_image FROM members_info WHERE members_id='{$_SESSION['memberid']}'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $default_image = $row['default_image']; echo "<img src=\"$default_image\" height=\"120\" width=\"120\" alt=\" \" />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> user_auth.php <?php require_once("db_fns.php"); function login($user_name, $password) // check username and password with db // if yes, return true // else return false { // connect to db $conn = db_connect(); if (!$conn) return 0; $query = "SELECT * FROM members_info WHERE user_name='$user_name' AND password=MD5('$password') LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result) > 0) { $row=mysql_fetch_assoc($result); $memberid = $row['members_id']; $_SESSION['memberid'] = $memberid; $last=$row['last_visit']; #Display date as 5/8/2007 format $last = date('n/d/Y', strtotime($last)); $_SESSION['last']=$last; //use the session variable to hold the previous last visit date. $sql="UPDATE members_info SET last_visit=NOW() WHERE members_id='{$_SESSION['memberid']}'"; //now only update it $result2=mysql_query($sql); }else{ echo "No results found"; return false; } }else { echo "Query failed<br />$query<br />" . mysql_error(); return false; } return true; } ?> db_fns.php <?php // connects to the database function db_connect() { $result = mysql_pconnect("localhost"/*, " ", " "*/); if (!$result) return false; if (!mysql_select_db("trust_me")) return false; return $result; } // structure of the database /* drop table if exists members_info; create table members_info(members_id int unsigned not NULL auto_increment primary key, user_name varchar(25) not NULL, first_name varchar(25) not NULL, last_name varchar(25) not NULL, gender varchar(6) not NULL, birth_month varchar(9) not NULL, birth_day tinyint(2) not NULL, birth_year int(4) not NULL, contact_number varchar(10) not NULL, email_address varchar(100) not NULL, register_date datetime not NULL default'0000-00-00',password varchar(32) not NULL, last_visit datetime not NULL default'0000-00-00',default_image varchar(50) not NULL); */ ?> container_fns.php <?php // I can include this file in all of my files // this way, every file will contain all of the functions require_once("db_fns.php"); require_once("user_auth.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54486-question/#findComment-270284 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.