Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

What kind of timestamps are these?

12314
12345
12356
12367
12378
12456
12859

Adbot
ADBOT LOVES YOU

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

It's in a MySQL table that I don't have access to yet. All I have is a printout of what the table looks like.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

How can I improve this system of including/defining?

code:
define('ROOT_PATH', '/home/username');	// set the ROOT_PATH here
define('BACKYARD', '/lawn');		// set the backyard path here
include_once(ROOT_PATH . BACKYARD . '/config.inc.php');
include_once(ROOT_PATH . BACKYARD . '/skin.php');
include_once(ROOT_PATH . BACKYARD . '/db.inc.php');
include_once(ROOT_PATH . BACKYARD . '/error.inc.php');
I've set up 3 pages so far but I'm worried about making changes for when we go live...

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

How do I search for <br /> with preg_match_all?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Can I assume that #\n# will find all new lines?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

e: php4

I've got a $variable filled with a large amount of text separated by new lines (\n).

I want to split the $variable into two giant chunks after a certain amount of lines, is that possible?

I thought about using split() but that would just make a new array value for every sentence and that might not be an effective way of doing it.

Thanks

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Didn't know where to put this question but since I'm writing in PHP...

Should I stick to using a timestamp like this YYYY-MM-DD or use the Unix timestamp?

Is it okay to use both (eg. one for one system and one for another)?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

So last night I wrote a fairly simple calendar function. It seems to work fine but it looks inefficient. Keeping within the same principles... how would you make it better?

php:
<?
    function getMonth($month, $year)
    {
        $first        = mktime(0, 0, 0, $month, 1, $year);
        $start_day    = date('D', $first);
        $day_offset    = $this->getDayOffset($start_day);
        $days        = cal_days_in_month(0, $month, $year);

        $block = 0;
        $block_limit = 35;

        if($day_offset == 6)
            $block_limit = 42;

        while($day_offset > 0)
        {
            $this->days[$block] = null;
            $day_offset--;
            $block++;
        }

        $day = 1;

        while($day <= $days)
        {
            $timestamp = mktime(0,0,0, $month, $day, $year);
            $this->days[$block]['day'] = $day;
            if(isset($this->events[$timestamp]))
            {
                $this->days[$block]['venue']    = $this->events[$timestamp]['venue'];
                $this->days[$block]['location']    = $this->events[$timestamp]['location'];
            }
            $day++;
            $block++;
        }

        while($block < $block_limit)
        {
            $this->days[$block] = null;
            $block++;
        }
    }?>


The output is being stored in a multidimensional array.

Acer Pilot fucked around with this message at 22:28 on May 8, 2008

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Begby posted:

The first thing to do to make this better is to maybe comment it then tell us what it is supposed to do and why.

Sorry about that.

php:
<?
    function getMonth($month, $year)
    {
        $first        = mktime(0, 0, 0, $month, 1, $year); // first date of the month in unix time
        $start_day    = date('D', $first); // starting 'day' eg. mon, tues, wed, etc
        $day_offset    = $this->getDayOffset($start_day); // how many days from sunday does the calendar start
        $days        = cal_days_in_month(0, $month, $year); // how many days in this month?

        $block = 0; // init the block count
        $block_limit = 35; // how many days to include on the calendar with offsets

        if($day_offset == 6)
            $block_limit = 42; // exception the month starts on a saturday

        // add all the empty blocks before the first day
        while($day_offset > 0)
        {
            $this->days[$block] = null;
            $day_offset--;
            $block++;
        }

        $day = 1;

        // add all the days to the calendar
        while($day <= $days)
        {
            $timestamp = mktime(0,0,0, $month, $day, $year);
            $this->days[$block]['day'] = $day; // current day number
            if(isset($this->events[$timestamp])) // there's an event on this day
            {
                $this->days[$block]['venue']    = $this->events[$timestamp]['venue'];
                $this->days[$block]['location']    = $this->events[$timestamp]['location'];
            }
            $day++;
            $block++;
        }

        // add more blocks to make calendar symmetrical.
        while($block < $block_limit)
        {
            $this->days[$block] = null;
            $block++;
        }
    }?>

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

cal_days_in_month() is fun too.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

What's the best or easiest way to add language support? By language support I mean we can display an error message in English or maybe French rather than just English.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

So how do I disable PHP in one specific folder (for security reasons)?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Mine GO BOOM posted:

In .htaccess:
code:
AddType text/html .php

I'm more worried about being hit by PHP files disguised as images. Is there a way to get the engine to ignore a specific folder? I tried the php_admin commands in an htacess but it just gave internal server errors.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Bruno_me posted:

'php_flag engine 0' should do it

Doesn't seem to be working. Do I have to change any settings on the server?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Lacithe posted:

In Apache you need to have AllowOverride Options (or All).

I'm so confused, it's already set to All. I'm running on mod_php. :confused:

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

I saw a thread about this a long time ago but I can't find it anymore so...

How do you do the authentication system like they have on AwfulYearbook.

If you don't know what I'm talking about, it's where a website checks to see if you have a specific line of text written onto a certain page.

I hope that made sense.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Zorilla posted:

Probably include an activation token in the table of users. A random MD5 ought to do the job.

I was wondering more on how they did the actual checking of the website.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Use some regex on it? I'm sure a regex genius will post how to do this in a few mins.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

So I want to write a small/simple/safe search engine for our MySQL database. How would I start this off?

The fields I want to search are varchar(64) and longtext.

Any specific links or tutorials?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Bruno_me posted:

Assuming you're using MyISAM, and you don't have more than 500,000 or so rows to search through, Fulltext indexes are the way to go. You pretty much add the index and write a query like this:

code:
SELECT * FROM `table` WHERE MATCH(column[,2ndcolumn...]) AGAINST('search terms');

That was pretty easy to setup and it was fun. Thanks.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

So I'm trying to implement Dijkstra's algorithm for a side project I'm working on but I'm having difficulty grasping how the whole thing works...

I've setup a map where each location a player can be go to is identified as a sector. Each sector knows whether it connects to any of the sectors to the north, east, south, or west of it.

In MySQL, the table for sectors looks like this:

table: sectors
sector_id | north | east | south | west

If the current sector connects with another (eg. north) then it would have the sector_id in that field. Otherwise the value of 0 would be in the field.

Would the connecting sectors be the vertices?

To illustrate it, the map array might look something like this:

code:
array( '1' => array( 'north' => 0, 'east' => 2, 'south' => 6, 'west' => 0) );
array( '2' => array( 'north' => 0, 'east' => 3, 'south' => 7, 'west' => 1) );
array( '3' => array( 'north' => 0, 'east' => 0, 'south' => 8, 'west' => 2) );
array( '6' => array( 'north' => 1, 'east' => 0, 'south' => 0, 'west' => 0) );
array( '7' => array( 'north' => 2, 'east' => 8, 'south' => 0, 'west' => 0) );
array( '8' => array( 'north' => 3, 'east' => 0, 'south' => 0, 'west' => 7) );

1 - 2 - 3
|   |   |
6   7 - 8
edit: drew that wrong

Acer Pilot fucked around with this message at 00:34 on Jul 11, 2008

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Does anyone have any idea on how to efficiently create a random maze via PHP?

I found this one but I'm not quite sure on how to make it better. It takes quite awhile for it to make 20x20 maze as it seems to make them by going through every cell in the grid.

Anyone happen to know the name of the formula that makes up a maze?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Jam2 posted:

I am catching my rear end to enable the gd library in php5.

extension=php_gd2.dll is uncommented.

Apache has been restarted several times since.

Is there anything else that needs to be done to get the gd library enabled? Do I have to change anything in Apache?

Here's my phpinfo: http://dev.babaya.net/test.php

Is there a specific error message and did you compile it with GD enabled?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

I want to keep track of all the places somebody's been on a map in an array (possibly).

What would the best way to store this on MySQL? Note: they may not be visiting these places in order. The places will just be represented by numbers.

I was thinking textfield in the database separated by commas. But that doesn't sound so efficient.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Thanks but unfortunately we don't have PHP5 installed yet :(.

How crappy and or efficient are serialize and unserialize?

The array may reach up to 375 items per person and map so I'm a little worried about how well it would handle if there are, worst case, 5000 users and 15 maps.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

MrEnigma posted:

You can install the JSON stuff through PECL if you want (I believe this will work through PHP4), or ZendFramework has one also.

serialize will work, but then you need to write it out to a file each time. Which will work, but if you have easy access to a mysql db, you should set it up in there.

It could get kind of messy, but you'd have multiple tables.
Users (which contains a users_id, datetime column, place_id, rating, etc)
Places (which contains the places_id, name, location, features about that place, etc)

What are your thoughts on just using implode/explode?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Is this:
php:
<?
    function check_explored($sector, &$map)
    {
        $explored = $this->search_explored(0, (count($map)-1), $sector, $map);

        if($explored < 0)
            return -1;

        return $map[$explored];
    }

    function search_explored($start, $end, $key, &$map)
    {
        if($start > $end)
            return -1;
    
        $mid = round(($start + $end)/2);
    
        if($map[$mid] == $key)
            return $mid;
        elseif($key < $map[$mid])
            return $this->search_explored($start, ($mid-1), $key, $map);
        return $this->search_explored(($mid+1), $end, $key, $map);
    }?>
Faster and or more efficient than in_array?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Gotcha.

Now for another question I haven't seen in awhile...

How should I store passwords in MySQL? The only way I know of is to MD5 it with a salt. How would a security conscious goon do it?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Zorilla posted:

That's pretty much it, I think. You could make the login process done through SSL or use Javascript to MD5 the password on the client side so it isn't sent out cleartext, but you're on the right track so far.

Well I did get a free SSL certificate from Namecheap a week or two back so I'll give that a shot as well, thanks.

MononcQc posted:

Use SHA-1. Otherwise, salting it is pretty much the right thing to do.

Is there a huge difference between MD5 and SHA-1 other than the bit lengths? I would like to try something new, SHA-1 in this case, but I would like to keep our user accounts fairly safe from tampering.

Thanks for help so far goons.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

So would you add a salt before and after?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

I have a div with a set width and height and a background image.

How do I place images (in other divs possibly) randomly inside the main div? I want to try doing this without them overlapping.

If PHP is overkill, could I do this with Javascript?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

How can I make this simpler and or better?

php:
<?
if($curr_sector < $start_of_line)
    $curr_sector = -1;
elseif($curr_sector >= $start_of_line + $size)
    $curr_sector = -1;
elseif($curr_sector > $end_sector)
    $curr_sector = -1;?>

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

KuruMonkey posted:

php:
<?

if(($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)) || ($curr_sector > $end_sector))
{
  $curr_sector = -1;
}
?>
And put thise 3 sub-clauses in the order 'most likely to be true in normal usage' first.

(did you really intend the result of all three options to be the same?)

Also consider if its simpler / clearer to test for the cases where you shouldn't be setting curr_sector to -1, and test for that negated:

php:
<?
if(!(SIMPLER_CLAUSE))
{
  $curr_sector = -1;
}
?>
Lastly, if you're using -1 as an 'not valid' value in a variable that normally stores numbers, get into the habit of using FALSE for that - then test for it explicitly:
php:
<?
$val = myfunc();
if($val !== FALSE && YOUR_OTHER_TESTS_ARE_PASSED)
{
  // use $val knowing its valid
}
?>
So; working on the assumption that this is a test for the current sector being valid to use and then setting it to a value that (maybe) causes a loop to terminate... (also took a stab at what might be most likely to be the "normal" fail condition)

php:
<?

if(($curr_sector > $end_sector) || ($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)))
{
  $curr_sector = FALSE;
}
?>
which might go nicely with a while($curr_sector !== FALSE) {} loop

Oh god, thanks.

None of this was registering last night.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

With an array like this:

php:
<?
Array
(
    [0] => Array
        (
            [0] => -3
            [1] => -2
            [2] => -1
            [3] => 0
            [4] => -1
        )

    [1] => Array
        (
            [0] => -3
            [1] => -2
            [2] => -1
            [3] => 0
            [4] => -1
        )

    [2] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 1
            [3] => 2
            [4] => 3
        )

    [3] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 26
            [3] => 27
            [4] => 28
        )

    [4] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 51
            [3] => 52
            [4] => 53
        )

)?>
Would it be very memory/cpu intesive if I looped through this and checked for conditions?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Zorilla posted:

PHP has quite a few functions for searching for things in arrays or running callbacks for things in arrays, etc. Would in_array() work for what you need?

I actually need to run through everything in the array and output something, if the value is less than 1 it's supposed to output something as well.

I'm thinking two for loops and some conditional statements but that doesn't sound too memory efficient.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Begby posted:

Looping through an array is not that memory intensive in php. You can probably increase that array to include a couple of thousand elements and not notice too much of a hit. Try creating a loop from 1 to 10,000 that generates an array like this, then loop through it and see how much of a difference it makes.

Bottlenecks typically appear in database queries and file access.

Thanks to all for the replies.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Are you using pconnect? Also the page might be pushing your server loads up. You might want to try tinkering with the my.cnf

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Why is this:

php:
<?
while ( preg_match( "#<span style=['\"]font-size:?(.+?)pt;?\s+?line-height:?\s+?100%['\"]>(.+?)</span>#is", $txt ) )
?>
Not catching this:
<span style='font-size:10pt;line-height:100%'>[color=red]Click everyday & feed the hungry for free!:[/color]</span> <span style='font-size:10pt;line-height:100%'>thehungersite.com</span>

Acer Pilot fucked around with this message at 04:28 on Dec 8, 2008

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Zorilla posted:

Uncheck "Automatically parse URLs" and edit those out.

That didn't work :( Bug on the forum?

jasonbar posted:

I don't know if this is why, but your "\s" needs to be "\\s"

I'll give it a shot in a second.

edit that didn't work. HEre's more of the code

php:
<?
            while ( preg_match( "#<span style=['\"]font-size:?(.+?)pt;?\s+?line-height:?\s+?100%['\"]>(.+?)</span>#is", $txt ) )
            {
                $txt = preg_replace_callback( "#<span style=['\"]font-size:?(.+?)pt;?\s+?line-height:?\s+?100%['\"]>(.+?)</span>#is" , array( &$this, 'unconvert_size' ), $txt );
            }?>

Acer Pilot fucked around with this message at 04:35 on Dec 8, 2008

Adbot
ADBOT LOVES YOU

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Zorilla posted:

...gonna use this double post to break up my thoughts...

This expression should be closer to what you need, I don't know if it will work, but it seems to check out on regextester.com:

/<span style=(['"])font-size:\s*\d+(?:pt|%|em|px)\s*;\s*line-height:\s*\d+(?:%|em|px);?(\1)>(.*?)<\/span>/is

In PHP and Javascript, I've always seen regexes start and end with /, so your use of # may have been a problem unless there's something I don't know.

edit: I just found this, which is probably the most helpful thing I've found to date on learning regular expressions.

Hm. I got an error about the first ] (it being unexpected). Regex continues to hurt my brain.

Thanks for trying though.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply