Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2006, 04:21 AM
Registered User
 
Join Date: Dec 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add lines in php page automatically

hi

I have a folder where a number of PHP files are added periodically. I want to add 3 or 4 lines of code (for user/session authentication) automatically to these files.

is there a way ?

Saifi
 
Old December 19th, 2006, 04:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hai. include a txt or php file, the write that text file.
here is the code

include("code.php");

to write code.php follow the code
---------------------------------codecreater.php----------

$myFile = "code.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "write your user/session authentication details here \n";
fwrite($fh, $stringData);
fclose($fh);



surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old December 19th, 2006, 05:03 AM
Registered User
 
Join Date: Dec 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is a little bit complex.

I have a main php page that opens after the user authentication from a MYSQL Table. This main php page has some links that open the html files from a folder on my webserver. These html files are software generated and I am unable to alter through that software.

I took a code from www.reducedcomplexity.com that automatically change the extension of html files from a folder to php.

Now I have to put some lines (for user authentication) on each of the files present in a folder.

To edit each of the file is a lengthy process. Is there a way that I automatically read all files from a specific folder and add some php lines in the file.



Saifi
 
Old December 19th, 2006, 05:19 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hai,
you can read those files using include or require,
see the sample

<?php
include("c:\page.html");
?>

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old December 19th, 2006, 05:22 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

Hi,
List files from a folder use following code

<?php
 $imgdir = '/public_html/logs/'; // the directory, where your images are stored
   $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
  $dimg = opendir($imgdir);
  while($imgfile = readdir($dimg))
 {
  if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
  {
    $a_img[] = $imgfile;
   sort($a_img);
    reset ($a_img);
   }
   }
   $totimg = count($a_img); // total image number
   for($x=0; $x < $totimg; $x++)
   {
   $size = getimagesize($imgdir.'/'.$a_img[$x]);
    // do whatever
   $halfwidth = ceil($size[0]/2);
   $halfheight = ceil($size[1]/2);
   echo 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';
  }
   ?>

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old December 20th, 2006, 04:50 AM
Registered User
 
Join Date: Dec 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear surendran,

thanks for ur reply.

I want to know one thing more. when I open files from a folder, how i will add the code of some lines (for authentication) on each file.

briefly, I want to add following lines on top of each file in a folder without opening each file manually.

<?php session_start();
if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
    header('Location: http://localhost/index.php');
    exit;
}
?>

I hope for solution from ur end

Saifi
 
Old December 22nd, 2006, 12:13 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hai,
open your file using a following code, it will add your string top of the page.
$fh = fopen("filename", 'r+') or die("can't open file");

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML tags to automatically hang bulleted lines? cJeffreywang ASP.NET 2.0 Basics 1 February 6th, 2008 02:12 AM
How to Add Primery Key value Automatically kamran Yousaf Bk C# 1 October 6th, 2007 04:39 PM
Add lines in php page automatically saifi4u PHP How-To 1 December 19th, 2006 04:42 AM
Add few Lines of code to All new aspx pages. gadhiav ASP.NET 1.0 and 1.1 Professional 3 October 19th, 2005 02:33 PM
add row no automatically Jane SQL Language 9 June 28th, 2004 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.