1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with a PHP script, is this possible?

Discussion in 'PHP' started by saintdw, Jun 12, 2005.

  1. #1
    I want a script to open .htaccess and add an entry when i add a page.


    For example:

    I add a page called /page/dog-food/innova-dog-food-review and the script adds the following line: 5 and 9 being ID numbers from database.

    RewriteRule ^page/(dog-food/|dog-food)/(innova-dog-food-review/|innova-dog-food-review)$ /index.php?page=5&id=9
    SEMrush

    How hard would this be to accomplish?
     
    saintdw, Jun 12, 2005 IP
    SEMrush
  2. jbw

    jbw Peon

    Messages:
    343
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It should not be too hard to do directly. Are you one shared hosting though? If so you may not want to do it directly, as this opens your .htaccess files to be writable by the web server, and thus anyone else on the system (unless your host is using a suexec thing to run php in). There are workarounds, and/or if you are using a VPS or dedicated server it matters less.
     
    jbw, Jun 12, 2005 IP
  3. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah its on shared =\
     
    saintdw, Jun 12, 2005 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I hadn't thougt about it before, but you might be able to use php's ftp functions to make your .htaccess writeable, make your changes, and move the permissions back. That would make it safer on a shared host.
     
    exam, Jun 12, 2005 IP
  5. jbw

    jbw Peon

    Messages:
    343
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Almost, but now you would need to encode that php, or you just put you pw in a readable file for all the users on the host.

    You need to have the file with the pw readable by you only or encoded. Actually a bit safer way to do it , is to have a suid exe out of the directory tree that the web pages come from and exec that to do the writing to the file. At least this does not have your pw in a file encode or not, and your pw is not being sent in clear text for the ftp login in. You could then at least limit the what write actions are allowed to what that exe supports. You could probably also use session ids passed through shmem to add another layer of protection.
     
    jbw, Jun 13, 2005 IP
  6. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #6
    To answer your original question - about pulling numbers out of a database with mod_rewrite, No you can't. Not with a typical implementation anyways.

    You can use a rewritemap - calling an external program to return the url or conversely just build a rewritemap yourself in a text file.

    The problem with using a database for a rewrite rule is in the timing of everything. Opening a text file is a relatively quick task. Opening up a database connection, querying for data on that connection, computing a result and then closing the connection on every visit to your site can get system intensive and if there is high traffic you may run into memory issues.

    If you are doing a redesign, a rewritemap using a straight text file or db hash is the way to go.
     
    nevetS, Jun 13, 2005 IP
  7. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah Im leaning toward rewritemaps as well. Will have something to do at work now :D
     
    saintdw, Jun 13, 2005 IP
  8. mushroom

    mushroom Peon

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I ran accross this the other day booked maked it but have not downloaded it yet.

    "AuthClassed is a class, written in PHP, to implement a simple authentification scheme for database-oriented webpages. Using AuthClassed is as simple as putting a single code line at the top of the webpage you would like to protect."

    https://sourceforge.net/projects/authclassed/
     
    mushroom, Jun 13, 2005 IP
  9. ro_core

    ro_core Peon

    Messages:
    6
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You can write to the .htpass or .htaccess safely even if you're on shared hosting, as long as your php script is running as cgi as your user.

    The second method I use to have dynamic lookups defined by the url is to have all matches passed by mod_rewrite to a php file which looks up into the db to see what page to generate.

    It works like this:

    mod_rewrite detects match, sends to view.php with match as param
    view.php takes match and looks it up into db and generates associated page
     
    ro_core, Jun 13, 2005 IP
    mushroom likes this.
  10. ro_core

    ro_core Peon

    Messages:
    6
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Following up on my last post:

    Instead of:
    RewriteRule ^page/(dog-food/|dog-food)/(innova-dog-food-review/|innova-dog-food-review)$ /index.php?page=5&id=9

    You have:
    RewriteRule ^page/(.+) /index.php?match=$1

    Then you take match, look it up in the db, and generate the associated page.

    Don't forget to use [L] so mod_rewrite doesn't check more rules.
     
    ro_core, Jun 13, 2005 IP
    nevetS likes this.