Notices
 

Python Script for modifying file contents

For page specific messages
For page author info

Hello Friends,

Presently, I am working with PHP (Specially modifying some script which is already written by some one else). I have to add code for session expire after 30 minutes. It is about more than 70 files in which i have to add following code segments :-

These lines go inside PHP Tag

$expireAfter = 30;

 //Check to see if our last action session

//variable has been set.

if(isset($_SESSION['last_action'])){

//Figure out how many seconds have passed

//since the user was last active.

$secondsInactive = time() - $_SESSION['last_action'];

//Convert our minutes into seconds.

$expireAfterSeconds = $expireAfter * 60;

//Check to see if they have been inactive for too long.

if($secondsInactive >= $expireAfterSeconds){

//User has been inactive for too long.

//Kill their session.

session_unset();

session_destroy();

header(Location: login_page.php);

}

}

//Assign the current timestamp as the user's

//latest activity

else{

$_SESSION['last_action'] = time(); 

 

 

Where as this line go to Head Tag

<meta http-equiv=refresh content=1805>

 

For this purpose, I have written a Python Scripts. It reads file from a given location, create file with the same name, write above code at appropriate place. I am also attaching source code with thi post.  

 

import glob

import os

 

os.chdir("/home/suraj/Downloads/Reviewers/")

filename_arr={}

i=0

for files in glob.glob("*.php"):

    filename_arr[i] = files

    i= i+1

print i;

c=0;

for key,value in filename_arr.items():

    e=open("Reviewers/"+filename_arr[c],"w")

    print "name of opened file for writing"

    print filename_arr[c];

    f=open(value,"r")

    j=0

    while True:

        l=f.readline()

        l1=len(l)

        e.write(l)

        if l1 >= 7:

            if l[1]=="/" and l[2]=="h" and l[3]=="t" and l[4]=="m" and l[5]=="l":

                print "end reached break run"

                break

        if j==7:

           e.write("$expireAfter = 30;\n //Check to see if our ""last action"" session\n//variable has been set.\nif(isset($_SESSION['last_action'])){\n//Figure out how many seconds have passed\n//since the user was last active.\n$secondsInactive = time() - $_SESSION['last_action'];\n//Convert our minutes into seconds.\n$expireAfterSeconds = $expireAfter * 60;\n//Check to see if they have been inactive for too long.\nif($secondsInactive >= $expireAfterSeconds){\n//User has been inactive for too long.\n//Kill their session.\nsession_unset();\nsession_destroy();\nheader(""Location: login_page.php"");\n}\n}\n//Assign the current timestamp as the user's\n//latest activity\nelse{\n$_SESSION['last_action'] = time();\n")

  #          break

        if j==19:

            e.write("<meta http-equiv=""refresh"" content=""1805"">")

        j=j+1

#e.write("\n")

#e.write(str(c))

#e.write("\n")

    e.close()

    f.close()

    c=c+1

    print c

Attachments for download: 

Exclude node summary : 

n
0
 
X