Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing page properties without database [closed]

Tags:

php

I have a homepage where I have a number of variables such as a phone number, open times etc and I cant think of a way to store multiple variables permanently other than in a database and I dont want to make a table just to store 1 row's worth of properties. I was thinking of possibly using a text file but I dont really know.

like image 746
Batzz Avatar asked Dec 08 '22 05:12

Batzz


2 Answers

This is exactly the kind of thing that CMS solutions such as Joomla are built for. As soon as you start going down this route with any decent amount of data you probably want to start looking at something like that.

However, if you really want to, you can go down the route of serialising whatever you want to store and putting it into a text file.

For example, setting it up:

$configuration = array( 'PhoneNumber' => '123456789',
                        'OpenTimes'   => 'Monday 9:00 to 12:00' );

file_put_contents( 'configuration.txt', serialize( $configuration );

Getting it back out:

$configuration = unserialize( file_get_contents( 'configuration.txt' ) );

The file can contain much anything - string or objects or whatever. Just be aware that if you store objects you'll need to ensure that you may need to include their class definition before you re-load them. And if you store a resource (like a DB connection - I know, unlikely!), you'll need to reconnect or reopen the resource.

Alternatively - if it really is just a series of simple strings, you might want to look at ini files.

http://php.net/manual/en/function.parse-ini-file.php

Let's say you have configuration.ini file with the content:

[address_info]
phone_number = 123456789
open_times = Monday 9:00 to 12:00

You can then load it with:

$configuration = parse_ini_file("configuration.ini", true);

And then reference with:

$configuration['address_info']['phone_number']
like image 56
Rob Baillie Avatar answered Dec 11 '22 09:12

Rob Baillie


You may use PHP's require_once if you were to store the page information in another PHP file as variables.

This will act like a database (sort of).

If this was your PHP file with your variables (let's call it a.php):

<?php
    $phoneNumber = "1234 5678";
    $openTimes = "09:00-17:00";
?>

You would use require_once("/a.php"); to utilize the variables in the file and use them like this:

<?php
    require_once("/a.php");
    echo "Phone Number: " . $phoneNumber;
?>

require_once is an extremely useful feature of PHP, and can be used for many purposes.

Potential use examples of require_once:

  • Maintaining a header across an entire website from just the one header PHP file
  • Connecting to a database where the require_once'ed file is situated in a secure non-public area of your website (decreases the chances of someone 'hijacking' your database)
  • Storing easily-changeable variables where a database is not required.
like image 37
AStopher Avatar answered Dec 11 '22 10:12

AStopher