Jump to content

doddsey_65

Members
  • Posts

    902
  • Joined

  • Last visited

  • Days Won

    9

Everything posted by doddsey_65

  1. sorry i realised my mistake. When hovering on the link it displays the ? in the path on the bottom of the browser but when clicked it does print the right value in the url. My htaccess file was preventing me from seeing this until now.
  2. when i use urlencode on my links it adds a + where there are spaces but it doesnt turn the special characters into their ASCII equiv. example. This works fine: $link = urlencode('testing this?'); echo $link; // returns testing+this%3F but this doesnt work: $link = urlencode('testing this?'); echo '<a href="'.$link.'">Link</a>'; // returns testing+this? why isnt it converting the special characters when its within a hyperlink?
  3. So then how would you do it? how would you get $config['asf_root'] from the config file and inject it into a class?
  4. thanks, i have used private on the actual script just forgot to make the example ones private. So theres no problem with getting the config and other variables that way?
  5. so to get my config variable within a class could i use this: class mainCore { public static function setVariable() { return $config = 'Config Var'; } } class testClass { public $config; public function __construct() { $this->config = mainCore::setVariable(); echo $this->config; } } sorry if it's still wrong, im just trying to grasp this concept. Ive only ever used global keyword.
  6. okay this is what im doing now but how do i run a query? class db_pdo extends PDO { public $conn; public function __construct($connectionNew) { $this->conn = $connectionNew; } public function getConnection() { $dsn = "mysql:dbname=asf_forum;host=localhost"; $user = "************"; $pass = "************"; $connection = parent::__construct($dsn, $user, $pass); return $connection; } class mainCore extends db_pdo { public function getInstance() { self::$instance = new mainCore(parent::getConnection()); return self::$instance; } i have tried using self::$instance->query but it comes up with the error Fatal error: Call to a member function query() on a non-object Any advice? it is connecting to the database btw.
  7. okay i have amended the database class like so: class db_pdo extends PDO { public $query_count = 0; public $execution_time = 0; public $query_list = ''; public $conn; static $hostname = 'localhost'; static $database = 'asf_forum'; static $username = '********'; static $password = '********'; public function __construct($conn) { $this->conn = $conn; } function getConnection() { $db_connection = parent::__construct("host=" . self::$hostname . " dbname=" . self::$database . " user=" . self::$username . " password=" . self::$password); return $db_connection; } but then what do i do in the classes i want to use the database connection? i added this to my forum class private static instance; public function getInstance() { if (!isset(self::$instance)) { self::$instance = new OtherClassNameHere(parent::getConnection()); } return self::$instance; } but im not sure what OtherClassNameHere is supposed to be.
  8. that all sounds good but i didnt understand any of it. Do you perhaps have a small example or is there a tutorial out there somewhere?
  9. i have several classes which have a private var $db. when i use a method that requires a db connection i connect to the database and assign it to $db. however this means connecting to the database in each method. Is there any way i can connect to the database in the main core class and then call this connection in other classes? i thought of connecting in the main class and then extending each other class off it but you cant use the private variable $db, just methods. Any tips? something like: class mainCore { public $db; public function __construct() { //connect to database } } class anotherClass extends mainCore { public function someFunction() { $this->db->query(); } } $asf = new mainCore;
  10. okay, i have a login page. When the user presses submit jQuery is used to send the post data to the login_process.php script, which fires the loginUser() method. if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['remember'])) { $user->loginUser($_POST['username'], $_POST['password'], $_POST['remember']); } this class method does all of the validation then outputs either 'success' or the error back to the jquery script. The problem is that i have this line in the class: $this->userData[] = $row; userData is public $userData = array(); its supposed to add the query result array to this array so i can call the info from other scripts like $user->userData['u_username'] but userData never gets populated with the query result. I am calling the new userClass via the initialize.php page which is called from every page. Could this be why? Here is the full user class: class userClass extends mainCore { private $db; /** * Hash Method * can be one string or multiple sepearated with comma * * @return array */ public $hash = 'sha1, md5, sha1, sha1'; public $start_hash; public $end_hash; public $cookieName; public $cookiePath = '/'; public $cookieValue; public $cookieTime = 9999999; /** * holds all the data for a logged in user * * @return array */ public $userData = array(); public $asf; public function loginUser($username, $password, $remember = 'no') { session_start(); $this->getConfig(); // return an error if there are blank fields if(empty($username) || empty($password)) { $this->error('Please fill in all fields'); } // connect to the database $this->db = new db_pdo( $this->config['db']['hostname'], $this->config['db']['database'], $this->config['db']['username'], $this->config['db']['password']); // convert the hash into an array $hash = explode(',', $this->hash); // the beginning of the hash srting $this->start_hash = implode('(',$hash); // the length of the hash // used to print right amount of closing brackets $hash_length = count($hash); // add an extra bracket which isn't added by implode $this->start_hash .= '('; // add the correct amount of closing brackets for($i=0; $i<$hash_length; $i++) { $this->end_hash = str_repeat(')', $hash_length); } // hash the password $password = $this->start_hash."'".$password."'".$this->end_hash; // query to match the user submitted info $lookup_query = "SELECT u_username, u_salt, u_password FROM ".TBL_PREFIX."users WHERE u_username = '$username' AND u_password = $password LIMIT 1"; // execute the lookup query $execute = $this->db->query($lookup_query) or die($this->db->printError($lookup_query)); // grab the query results $row = $execute->fetch(PDO::FETCH_ASSOC); // assign userData array if($row) { $this->userData[] = $row; // if remember me was checked add a cookie if($remember == 'yes') { $this->cookieName = 'uniqueId'; $this->cookieValue = md5($this->userData['u_username']); $cookie = setcookie($this->cookieName, $this->cookieValue, time()+$this->cookieTime, $this->cookiePath); } $_SESSION['logged_in'] = 'yes'; echo 'success'; return $this->userData; } else { $this->error('Invalid details'); } } }
  11. okay say i have an array $config which is never reassigned a different value. It holds all forum relative variables: $config['board_name'] = 'asf'; $config['board_root'] = './'; would i be okay using that as a global when i need to define the board root for a hyperlink. Because i cant use relative urls when using mod rewrite
  12. thanks for the help. TBL_PREFIX definition was just put there to make the code work.
  13. i have taken everything said into consideration and made an example of the final user class which contains a method for registering users. Let me know what you think and if there are any ways of improving this method. As i said it's just an example and i will be adding to it later. <?php define('TBL_PREFIX','asf_'); function __autoload($class) { include('./includes/'.$class.'.php'); } class user { /** * Will hold the database class * * @return void */ public $db; /** * Hash Method * * @return string */ public $hash = 'md5'; /** * Handles the registration of a new user * * @param array $user_info */ public function registerUser($user_info) { if(is_array($user_info)) { /** * connect to the database * * @param string $hostname * @param string $database * @param string $username * @param string $password */ $this->db = new db_pdo('localhost', 'asf_forum', '**********', '**********'); // escape the $user_info array $user_info = array_map('addslashes',$user_info); // hash the password switch($this->hash) { case 'md5': $user_info['password'] = md5($user_info['password']); break; case 'sha1': $user_info['password'] = sha1($user_info['password']); break; } // setup the insertion query $insert_query = "INSERT INTO ".TBL_PREFIX."users (u_username, u_password) VALUES ('".$user_info['username']."', '".$user_info['password']."') "; // execute the query $this->db->query($insert_query) or die($this->db->printError($insert_query)); } } } $user_info = array(); $user_info['username'] = "test'sUsername"; $user_info['password'] = 'google'; $user = new user; $user->registerUser($user_info);
  14. Thanks for the help. I now understand global and inclusion of database info into the class. However i still cant grasp static(sorry) so i will have t look into it further.
  15. Thanks for the reply. You mention that using global must be avoided. What about including the $linkvariable using global? So if i want to use a query within a class method i use global $link. Is that still okay? And does making the class variable static mean that it cannot be modified once assigned? That is why i set the username to static, because it wont be changed. Or am i thinking about that wrong? I will change the die statements to exceptions. Thanks for the help so far.
  16. ive created this small example class for users to set them up. Before i start to expand i would like some advice on whether its good or not. class user { static $username; static $email; public function __construct($username) { self::$username = $username; } public function setupUser($username) { global $link; $user_query = $link->query( "SELECT * FROM ".TBL_PREFIX."users WHERE u_username = '$username'" )or die($link->print_error()); $row = $user_query->fetch(PDO::FETCH_ASSOC); if(!$row) { die('Could not find info for user '.$username); } else { self::$email = $row['u_email']; } } } So if i wanted to print a users email i could use something like: $user = new user; echo $user::$email;
  17. ive never quite grasped OOP. I have used it thus far in cases such as the user system. I would hold info on the user in the user class so i could call it like so $user->email; or i could use $user->setup($uid) which would get the info for me. I have never used complex OOP. I havent looked at many tutorials. The example i posted was "ripped" from some code from an online forum. Thanks for the advice. even though my code does work procedurally i think i will definatly need a book or two on OO php. Any suggestions?
  18. to be honest i dont even know why i set it up like that. I saw it in someone elses code and tried to recreate it. I dont see the point of calling the newTemplate class within the asf class when the functions can all e added to the asf class in the first place. All my code on my forum works fine but is a mess. Thats why i have tried to get into OOP rather than procedural. But i suppose if the code works then that is the main thing.
  19. im trying to learn more on OOP so decided to try this code: class asf { public $_template; public function __construct() { $this->_template = new newTemplate; } } class newTemplate { public function setTemplate($template) { global $asf; $asf->_template = $template; } public function getTemplate() { global $asf; return $asf->_template; } } $asf = new asf; $asf->_template->setTemplate('default'); echo $asf->_template->getTemplate(); setTemplate() works but i get the following error on getTemplate(): Call to a member function getTemplate() on a non-object can anyone explain why and let me know how to fix it? Thanks.
  20. I am trying to display current online registered users and online guests. A user is defined as online by the online column in the database. if set to 1 they are online, if set to 0 they are offline. so when a user logs in i set this column to 1 to show they are online. but when a user is inactive i set it to 0. i do this using: if($user->last_active_time < time()-1800) { $link->query("UPDATE ".TBL_PREFIX."users SET u_online = 0 WHERE u_username = '".$user->user_name."'") or die(print_link_error()); } else { $link->query("UPDATE ".TBL_PREFIX."users SET u_online = 1 WHERE u_username = '".$user->user_name."'") or die(print_link_error()); } $user->last_active_time is set on every page load to the current time stamp(UNIX) however they are sometimes displayed as online or offline when they arent. Also for displaying guests i use: $session_time = time()-300; $query = $link->query("SELECT g_ip FROM ".TBL_PREFIX."guests WHERE g_ip = '".$_SERVER['REMOTE_ADDR']."'") or die(print_link_error()); $num_rows = $query->rowCount(); if($num_rows == 0) { if(!$user->user_name) { $link->query("INSERT INTO ".TBL_PREFIX."guests (g_ip, g_time) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$config['time_now']."')") or die(print_link_error()); } } if the IP address of the current visitor is not in the database and they are not logged in then it adds their ip to the database. but sometimes more guests are displayed then there actually are(tested this on local server so i know how many are online). Are there any better ways i could be doing this?
  21. the problem was this line: $result = $tag_query->fetch(); i should have been using $result = $tag_query->fetchAll(); to loop through the results in the foreach loop.
  22. does anyone know of a simple tutorial which will show me how to receive email sent via my localhost? i cant develop my script fully because of the email errors. thanks
  23. im trying to run a query which will get posts from the database that have tags similar to the current ones. Unfortunatly the echoed result isnt displaying as it should. $query = $link->query("SELECT t.*, p.* FROM ".TBL_PREFIX."topics t JOIN ".TBL_PREFIX."posts p ON (p.p_tid = t.t_tid) WHERE t.t_name = '".decode_url($_GET['t_name'])."' LIMIT 1") or die(print_link_error()); $row = $query->fetch(PDO::FETCH_ASSOC); $tag_query = $link->query("SELECT p_name FROM ".TBL_PREFIX."posts WHERE MATCH (p_tags) AGAINST ('".$row['p_tags']."' IN BOOLEAN MODE) ")or die(print_link_error()); $result = $tag_query->fetch(); foreach($result as $key => $value) { echo 'Name = '.$result[$key]['p_name'].'<br />'; } i have done a dump of result and it shows: $ => Array (2) ( ['p_name'] = String(12) "File Updates" ['0'] = String(12) "File Updates" ) but there should only be one item in this array(p_name). I dont know where the second is coming from. The query has pulled the right results, it just isnt displaying it right.
  24. im making a custom tag list where a user can enter tags into an input and seperate them with a comma. when they hit the comma key i want the name of the tag to be appended to a tag list. This is what i have so far but everytime a comma is inserted it adds all of the tags that are in the input field to the list rather than the one they have just added. $j('#post_tags').keyup(function(e){ if(e.which == 188) { var tags = $j(this).val().split(','); var length = tags.length; $j(tags).each(function(i){ $j('#tag_list').append(tags[i]); }); } });
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.