Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Custom CMS help!

Featured Replies

Hi all,

 

Im creating a set of classes for a custom CMS. I'm building the CMS in a way that the client will be able to define the database information (host, username, password, ect.) during installation (a page called install.php that submits a form with all this information). I have ran into a wall with this though. I want to be able to save the database info either as constants in the class or on the database itself, but if I store it on the database, how will I access it in order to use/connect to it later. So I'm thinking that making it a constant in the class might be the way to go, but I'm not sure. I also thought that it might be best to take the database information and write a new file on the server (config.php) and then include that in the class, but I'm not sure how to create a new file that way.

 

How should I go about this?

 

Thanks for all your help,

MrMicRipper

  • Author

Thanks a lot for your quick response rallport.

 

Yea the more I think about it, the more it seems like that would be the best possible way. now I need to figure out(thx google) how to write and save files with php.

 

Thanks again,

MrMicRipper

Hello there,

 

I'd suggest having a word writeable file called something like 'config.php' and have that file written too once the database information is entered. There should be a way to revert this file back to 755 once it has been written too, or you can ask the user to do so, and just use is_writeable() to complain if they don't do it.

 

Keeping the file outside of public_html isn't a bad idea, but some hosts restrict access to the web user (nobody, www, apache etc etc), so that it cannot access anything outside of the assigned directory.

 

A lot of scripts use the method I just suggested, and I believe the age old saying "If it isn't broken, don't fix it" is perfectly apt.

 

Hope that is helpful.

 

-- Edit --

 

For writing to the file, just create a variable with the contents of the file and append the input to it. file_put_contents() and file_get_contents() are the only real functions you need :p

  • Author

Thanks guys for all your help!

 

I managed to finally work this out. What I did was make a function (setSettings()) to write the database information to a new file on the server called 'config.php' and made another function (getSettings()) that reads that file and puts the configuration information in an array, then returns that array. Here are the functions for anyone else who might need them:

 

class CMS{

    var $dbSettings;

 

public function setSettings($dbhost, $dbuser, $dbpass, $dbname){

    $handle = fopen('config.php','w+');

    if($handle){

         $output = "<?php
         // Define database variables				


    	     \$DBHOST = '".$dbhost."';		
     \$DBUSER = '".$dbuser."';		
     \$DBPASS = '".$dbpass."';		
     \$DBNAME = '".$dbname."';

         ?>";

         if(!fwrite($handle, $output)){
              die("Could not write to file ".$handle.". Please try again later.");
         } 
    }

    fclose($handle);
}

 

public function getSettings(){
    $handle = fopen('config.php','r');

    while(!feof($handle)){
         $line = trim(fgets($handle));

  $line = explode("$", $line);

  $line = explode("=", $line[1]);

  if(($line[0] != '')||($line[1] != '')){
       $variable = trim($line[0]);
       $value = trim(substr($line[1],2,-2));

       $this->dbSettings[$variable] = $value;
  }	
    }					

    fclose($handle);

    return $this->dbSettings;
}

 

Example:

$cms = new CMS();
$cms->setSettings('localhost','mrmicripper','my_pass','my_db')
$settings = $cms->getSettings(); 
echo $settings['DBHOST']; //Returns localhost
echo $settings['DBNAME']; //Returns my_db

 

Thanks for all your help, and I hope this code could help someone else as well.

MrMicRipper

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.