Jump to content

newbtophp

Members
  • Posts

    631
  • Joined

  • Last visited

Posts posted by newbtophp

  1. Hey!,

     

    Just looking for some advice on developing a voting/rating algorithm.

     

    What is the most effective way to develop this?

     

    Right now, I am using minimalistic/very simple algorithm, I have two columns: number_of_votes and total_vote, and the score = number_of_votes/total_vote

     

    Problem is, it can be easily skewed if their are a low number of votes (1 vote of 5 will boost the score to 5).

     

    PS: They can vote from 1-5 (the max the score can be is 5)

     

    Thanks guys!

  2. Which is more reliable (functionality wise - not compatibility wise).

     

    Using Javascript (new Date(); function to detect user/visitor time automatically) or PHP's (Date and Time extension - DateTime:: - where the user/visitor selects their timezone themselfs)

     

    To retrieves the users/visitors time?

  3. Now would be a UNIX_TIMESTAMP but all dates and times in MySQL are stored as UNIX_TIMESTAMP's and just converted for display, also if you set the time via MySQL then you should use Now() if you set it via PHP time() then you could use time()

     

    loginTime is set using php's time()...

     

    so for checking the total users online, would the following work?

     

    <?php
    $time = time();
    $result = mysql_query("SELECT user FROM site_table WHERE loginTime > ($time - ( 15 *60 ))");
    //mysql_num_rows etc..
    

     

    Tested that and that works. solved. cheers MadTechie

  4. Now would be a UNIX_TIMESTAMP but all dates and times in MySQL are stored as UNIX_TIMESTAMP's and just converted for display, also if you set the time via MySQL then you should use Now() if you set it via PHP time() then you could use time()

     

    loginTime is set using php's time()...

     

    so for checking the total users online, would the following work?

     

    <?php
    $time = time();
    $result = mysql_query("SELECT user FROM site_table WHERE loginTime > ($time - ( 15 *60 ))");
    //mysql_num_rows etc..
    

  5. I am removing time from Now now the user, i could of added the time to the user (either works)

     

    here is the logical break down

     

    EDIT: (think of is as minutes past 12)

     

    if

    loginTime = 20minutes

    and

    Now = 30minutes

     

    then 

    loginTime > (Now - 15) =

    20 > (15) = true

     

     

    if

    loginTime = 10minutes

    and

    Now = 30minutes

     

    then 

    loginTime > (Now - 15) =

    10 > (15) = false

     

    Ok great, but one thing wouldn't now( ) be UNIX_TIMESTAMP() instead or $time ( and $time is assigned to $time = time(); )?

  6. Ok I've deleted the isOnline as its uneeded.

     

     

    So now I have loginTime which is UPDATEd with the current time() (timestamp) upon page load (its added at the top of every page within an include).

     

    How would I determine if a specific user is online, how would the query be (im guessing it would be structured like the example below but not sure how the WHERE bit will be)?

     

    SELECT user FROM site_table WHERE loginTime IS WITHIN 15 MINUTES FROM CURRENT TIME AND user = 'tester'

  7. Hey!

     

    I have a table...with a column isOnline which is set to 1 when logging in (using login.php)...and 0 when logging out (logout.php), its how I determine whos logged in and whos not (in statistics etc.)

     

    Now the problem is if they dont have the remember me enabled..and don't log themselfs out via logout.php (which sets the isOnline to 0)...and close their browser the $_SESSIONS's get destroyed (which means their physically not logged in)...and their isOnline remains at 1 (even though their logged out)

     

    Another problem..is if they login with remember me enabled (as they want to remain logged in)....and close their browser or whatever, isOnline will still remain as 1 (theirfore others will think their online).

     

    For your info:

    In login.php I do an UPDATE query adding the current time generated by time() into the loginTime column...and also setting isOnline to 1.

     

    So my question is how can I overcome this, I have the isOnline (1 = logged in, 0 not logged in) and loginTime (which contains the login timestamp)?

     

    Cheers

  8. Hi,

     

    Is this the correct way to go by setting cookies for my login 'remember' functionality?, I've posted the relevant login and logout code.

     

    Login:

     

    <?php
    session_start();
    
    //some code...
    
    if (isset($_POST['remember'])) {
    
    $expire = time() + 1728000; // Expire in 20 days
    
    $site_domain = 'phpfreaks.com'; //example
    
    $_SESSION['username'] = $_POST['username']; //don't worry this has been validated 
    
    $username = $_SESSION['username'];
    
    //some sql query to fetch code..
    
    $code = $row['code'];
    
    setcookie('username', $username, $expire, NULL,  ".$site_domain", NULL, TRUE);
    
    setcookie('code', $code, $expire, NULL, ".$site_domain", NULL, TRUE);
    
    }
    ?>
    

     

    Logout:

     

    <?php
    session_start();
    
    if (isset($_COOKIE['username'])) {
    
    $expire = time() - 1728000; // Expire in 20 days
    
    $site_domain = 'phpfreaks.com'; //example
    
    $username = $_SESSION['username'];
    
    //some sql query to fetch code..
    
    $code = $row['code'];
    
    setcookie('username', $username, $expire, NULL, ".$site_domain", NULL, TRUE);
    
    setcookie('code', $code, $expire, NULL, ".$site_domain", NULL, TRUE);
    
    }
    
    unset($_SESSION['username']);
    
    $_SESSION = array();
    
    session_destroy();
    ?>
    

     

    Please tell me if it looks good to go and/or any improvements.

     

    PS:

     

    I want it accessible on all areas of the domain (including subdomains and paths etc.), and HTTP ONLY (so js can't access it) - I believe I've set the right parems?

  9. The most common usage of the query string part of a url is to retrieve the part you want from within the $_GET array. Parsing the string yourself is almost always un-required.

     

    I'm quite sure your going about this the wrong way.

     

    so you mean this is wrong even if it works well on my part? I added this on the above script:

     

     

    $lang= curPageURL();

    $query = parse_url($lang, PHP_URL_QUERY );

     

    if ($query=="lang=zh-hans")

     

    Your going about it the long way. Whats wrong with....

     

    if ($_GET['lang'] == 'zh-hans') {
      // do whatever.
    }
    

     

    Of course, you should also check lang is set first....

     

     

    if (isset($_GET['lang'] && $_GET['lang'] == 'zh-hans') {
      // do whatever.
    }
    

     

    This is why I asked what exactly you where planning on doing with the data in my first reply. There really is no need to parse the url yourself.

     

    Your missing the closing parethisis for the isset on your code.

  10. I'm trying to create a function where you specify the $points it will then do some mathematical stuff with the $points (explain later) an return an array containing the $rank and $level.

     

    The mathematical stuff:

     

    It starts at 99 and when it reaches +199 it changes the key of the array.

     

    I'm not sure how to describe this theirfore i'll just provide examples:

     

    Example 1:

     

    If $points <= 99

     

    It will return an array containing $ranks[0] and $levels[0], would be the following:

     

    return array('Peon', 0);

     

    Example 2:

     

    If $points >= 100 && $points <= 299

     

    It will return an array containing $ranks[1] and $levels[1], would be the following:

     

    return array('Grunt', 1);

     

    Example 3:

     

    If $points >= 300 && $points <= 499

     

    It will return an array containing $ranks[2] and $levels[2], would be the following:

     

    return array('Berserker', 2);

     

    etc.. until it reaches the 7th key of the $ranks/$levels array

     

    Heres my code:

     

    <?php
    
    function rank($points){
    //their are 0-7 levels
    $levels = range(0, 7);
    
    //7 ranks
    $ranks = array('Peon', 'Grunt', 'Berserker', 'Tauren', 'Spirit Walker', 'Wind Rider');
    
    for(...) {
    return array($rank, $level);
    }
    }
    ?>

  11. I have an idea, which I'm trying to realise, im using the time() function to store the timestamp within my db...and within the db I have a timezone column (which contains the timezone selected by that user).

     

    So I'd like to display $row['timestamp'] (the stored timestamp generated by time()) to the specified timezone, how would that work?

     

    <?php
    //$timestamp = timestamp generated by the time() function
    //$timezone = the timezone to convert $timestamp too
    
    function convert_time($timestamp, $timezone) {
    //$new_timestamp = do something to adapt $timestamp to $timezone
    
    //return the formatted/readable date...
    return date('D M Y H:i', $new_timestamp);
    }
    
    ?>

  12. So you have loads of filenames, you want to change to SEO Friendly rewrites.

     

    Notepad++ and Find and Replace sorry

     

    I meant like is their something I can do so in the furture if I changed the way the seo urls where, I can easily change the urls within the code?, I can use variables - within a global/config file, but is their a better solution (more generic?)

  13. Is their something I can do in PHP (like some sort of function/config/array etc...) which would make the following process easier (examples appreciated)...

     

    I have an .htaccess file where im rewriting urls to seo friendly ones...now in the PHP script is their something I can place for urls are meant to be displayed/linked (like some sort of placeholder) which makes it easier for me to modify the urls within the PHP script, without constantly editing countless php sourcecode to just change the url?

     

    Example scenario:

     

    Say I had a php file (called submit.php):

     

    <?php
    echo 'Hey!
    Thanks for submiting the form, <a href="submit.php?form">click here</a> to go back!';
    ?>

     

    and my rewrite rule for submit.php?form was: /form

     

    How would I easily change the url within submit.php without editing submit.php directly?

  14. Im using func_get_args, but it gives an error?:

     

    Too few arguments?, when its correct (as the number of %s = the number of values within $args)

     

    <?php
    function sprintify() {
    global $urls;
    $args = func_get_args();
    $input = $args[0];
    unset($args[0]);
    return sprintf($urls[$input], implode(', ', $args));
    }
    
    echo sprintify('default', 46, 464, 46);
    
    /*
    For your information:
    $urls['default'] = '%s/%s/%s';
    */
    ?>

  15. Its their but the bbcode got parsed (view src to see lol), so i'll wrap it in a code tag so it doesnt:

    [b](.+?)[/b]
    
    //or
    
    [b](.*?)[/b]
    

     

    Which would be better within a preg_replace? (for a simple bbcode parser)

×
×
  • 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.