Need help to populate a structs array in perl - arrays

I need some help to populate an array made of structs in perl.
The data for the array comesfrom a .SH file with the following format :
108,Country,Location,ap17,ip_149,ssh,model,12/8/2020
The code I am using is as follows:
use strict;
use warnings;
use Class::Struct;
struct(Net_Node => [hostname => '$', dir_ip => '$', access => '$', user => '$', pass => '$']);
my $node = Net_Node->new();
my #nodes;
my $user = "hjack";
my $pass = 'butstalion';
my $line;
my #all;
my $counter=0;
open(my $fh, '<', "exaple.sh") or die "Failed to open especified file";
#system('clear');
foreach $line (<$fh>) {
#all=split(',', $line);
$node->hostname ($all[3]);
$node->dir_ip ($all[4]);
$node->access ($all[5]);
$node->user ($user);
$node->pass ($pass);
$nodes[$counter] = $node;
$counter++;
}
my $size = #nodes;
print "\n \n";
print ("array size = $size\n\n");
$counter = 0;
while ($counter < 20) {
print ($counter,"\n\n");
print ($nodes[$counter]->hostname,"\n");
print ($nodes[$counter]->dir_ip, "\n");
print ($nodes[$counter]->access, "\n");
print ($nodes[$counter]->user, "\n");
print ($nodes[$counter]->pass, "\n\n");
$counter++;
}
close($fh);
The output of this code is a populated array but only with the last element generated in the foreach loop, is there any way to populate this array with the data of the .SH file?
Thanks in advance
the data of the file is as follows
89,Country,Location,sw01,ip_10,ssh,model,12/8/2020
90,Country,Location,sw02,ip_18,ssh,model,12/8/2020
91,Country,Location,sw03,ip_26,ssh,model,12/8/2020
92,Country,Location,sw04,ip_27,ssh,model,12/8/2020
93,Country,Location,sw05,ip_28,ssh,model,12/8/2020
94,Country,Location,sw06,ip_29,ssh,model,12/8/2020
95,Country,Location,ap02,ip_13,ssh,model,12/8/2020
96,Country,Location,ap03,ip_12,ssh,model,12/8/2020
97,Country,Location,ap04,ip_20,ssh,model,12/8/2020
98,Country,Location,ap05,ip_14,ssh,model,12/8/2020
99,Country,Location,ap06,ip_15,ssh,model,12/8/2020
100,Country,Location,ap07,ip_16,ssh,model,12/8/2020
101,Country,Location,ap08,ip_17,ssh,model,12/8/2020
102,Country,Location,ap09,ip_18,ssh,model,12/8/2020
103,Country,Location,ap10,ip_19,ssh,model,12/8/2020
104,Country,Location,ap11,ip_24,ssh,model,12/8/2020
105,Country,Location,ap12,ip_25,ssh,model,12/8/2020
106,Country,Location,ap14,ip_27,ssh,model,12/8/2020
107,Country,Location,ap15,ip_37,ssh,model,12/8/2020
108,Country,Location,ap17,ip_149,ssh,model,12/8/2020

my $node = Net_Node->new();
...
foreach $line (<$fh>) {
...
$nodes[$counter] = $node;
}
creates a single Net_Node instance and overwrites its data in every iteration of the foreach loop. It sounds like you want to create a new instance for each line of the loop. So you should move your Net_Node->new() call inside the loop.
foreach $line (<$fh>) {
my $node = Net_Node->new();
...
$nodes[$counter] = $node;
}
With a simpler data structure like a native Perl hash, you could have appended a copy of the data structure to your list like
$nodes[$counter] = { %$node };
but I would be more reluctant to do that with an object, which might not even be represented internally as a hash reference.

Perhaps the code could be implemented in the following shape
Comment:
define structure of node
for readability define an array with descriptive fields of interest
read file line by line
place temporary the data of interest into a hash
create a new node on each iteration
fill the node with data from hash
store the node in an array
generate an output for nodes data
#!/usr/bin/env perl
#
# vim: ai ts=4 sw=4
use strict;
use warnings;
use feature 'say';
use Class::Struct Net_Node => [
hostname => '$',
dir_ip => '$',
access => '$',
user => '$',
pass => '$'
];
my #fields = qw/hostname dir_ip access/;
my($user,$pass) = qw/hjack bustalion/;
my #nodes;
while( <DATA> ) {
next if /^$/; # skip empty lines
chomp;
my %data;
my $node = new Net_Node;
#data{#fields} = (split(',',$_))[3..5];
$node->hostname( $data{hostname} );
$node->dir_ip( $data{dir_ip} );
$node->access( $data{access} );
$node->user( $user );
$node->pass( $pass );
push #nodes, $node;
}
say "\nTotal nodes = " . #nodes;
my $counter = 10;
for my $node ( #nodes ) {
last unless $counter--;
say "
Hostname : " . $node->hostname . "
Dir_IP : " . $node->dir_ip . "
Access : " . $node->access . "
Userid : " . $node->user . "
Passwd : " . $node->pass;
}
__DATA__
89,Country,Location,sw01,ip_10,ssh,model,12/8/2020
90,Country,Location,sw02,ip_18,ssh,model,12/8/2020
91,Country,Location,sw03,ip_26,ssh,model,12/8/2020
92,Country,Location,sw04,ip_27,ssh,model,12/8/2020
93,Country,Location,sw05,ip_28,ssh,model,12/8/2020
94,Country,Location,sw06,ip_29,ssh,model,12/8/2020
95,Country,Location,ap02,ip_13,ssh,model,12/8/2020
96,Country,Location,ap03,ip_12,ssh,model,12/8/2020
97,Country,Location,ap04,ip_20,ssh,model,12/8/2020
98,Country,Location,ap05,ip_14,ssh,model,12/8/2020
99,Country,Location,ap06,ip_15,ssh,model,12/8/2020
100,Country,Location,ap07,ip_16,ssh,model,12/8/2020
101,Country,Location,ap08,ip_17,ssh,model,12/8/2020
102,Country,Location,ap09,ip_18,ssh,model,12/8/2020
103,Country,Location,ap10,ip_19,ssh,model,12/8/2020
104,Country,Location,ap11,ip_24,ssh,model,12/8/2020
105,Country,Location,ap12,ip_25,ssh,model,12/8/2020
106,Country,Location,ap14,ip_27,ssh,model,12/8/2020
107,Country,Location,ap15,ip_37,ssh,model,12/8/2020
108,Country,Location,ap17,ip_149,ssh,model,12/8/2020
Output
Total nodes = 20
Hostname : sw01
Dir_IP : ip_10
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : sw02
Dir_IP : ip_18
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : sw03
Dir_IP : ip_26
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : sw04
Dir_IP : ip_27
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : sw05
Dir_IP : ip_28
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : sw06
Dir_IP : ip_29
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : ap02
Dir_IP : ip_13
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : ap03
Dir_IP : ip_12
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : ap04
Dir_IP : ip_20
Access : ssh
Userid : hjack
Passwd : bustalion
Hostname : ap05
Dir_IP : ip_14
Access : ssh
Userid : hjack
Passwd : bustalion
Otherwise a slightly different approach can be taken to achieve similar result without Class::Struct module
#!/usr/bin/env perl
#
# vim: ai ts=4 sw=4
use strict;
use warnings;
use feature 'say';
my #fields = qw/hostname dir_ip access/;
my($user,$pass) = qw/hjack bustalion/;
my #nodes;
while( <DATA> ) {
next if /^$/;
chomp;
my $node;
$node->#{#fields} = (split(',',$_))[3..5];
$node->#{qw/user pass/} = ($user, $pass);
push #nodes, $node;
}
say "\nTotal nodes = " . #nodes;
for my $node ( #nodes ) {
say "
Hostname : " . $node->{hostname} . "
Dir_IP : " . $node->{dir_ip} . "
Access : " . $node->{access} . "
Userid : " . $node->{user} . "
Passwd : " . $node->{pass};
}
__DATA__
89,Country,Location,sw01,ip_10,ssh,model,12/8/2020
90,Country,Location,sw02,ip_18,ssh,model,12/8/2020
91,Country,Location,sw03,ip_26,ssh,model,12/8/2020
92,Country,Location,sw04,ip_27,ssh,model,12/8/2020
93,Country,Location,sw05,ip_28,ssh,model,12/8/2020
94,Country,Location,sw06,ip_29,ssh,model,12/8/2020
95,Country,Location,ap02,ip_13,ssh,model,12/8/2020
96,Country,Location,ap03,ip_12,ssh,model,12/8/2020
97,Country,Location,ap04,ip_20,ssh,model,12/8/2020
98,Country,Location,ap05,ip_14,ssh,model,12/8/2020
99,Country,Location,ap06,ip_15,ssh,model,12/8/2020
100,Country,Location,ap07,ip_16,ssh,model,12/8/2020
101,Country,Location,ap08,ip_17,ssh,model,12/8/2020
102,Country,Location,ap09,ip_18,ssh,model,12/8/2020
103,Country,Location,ap10,ip_19,ssh,model,12/8/2020
104,Country,Location,ap11,ip_24,ssh,model,12/8/2020
105,Country,Location,ap12,ip_25,ssh,model,12/8/2020
106,Country,Location,ap14,ip_27,ssh,model,12/8/2020
107,Country,Location,ap15,ip_37,ssh,model,12/8/2020
108,Country,Location,ap17,ip_149,ssh,model,12/8/2020

Related

How can I avoid 2 loops and have my data stored in array in Perl

I have a script, which does count number of data in the input DATA and based on the count it will further proceed.
Below is my script, which is working as expected.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my (#array, #nodes);
my $count = 0;
while (<DATA>) {
push #array, $_;
$count = $1 if /Total number of data found :\s*(\d+)/;
}
print "Count:$count\n";
print "Array".Dumper(\#array);
if( $count > 0 ){
foreach my $ele(#array){
chomp $ele;
if( $ele =~ /NodeName : (\w+)/){
push #nodes, $1;
}
}
} else {
print "Count is less than 1.. Existing\n";
exit 0;
}
print Dumper(\#nodes);
__DATA__
Data : Network=R9,N=Node1
NodeName : Node1
DateTime : 05-08-2020 10:00:05
Data : Network=R9,N=Node2
NodeName : Node2
DateTime : 05-08-2020 10:00:10
Total number of data found : 2
But here I am using 2 loops, while and foreach.
while is used to capture the count and foreach is to iterate through each data elements which is been stored in #array while doing while operation.
Is it possible to have only 1 loop, either while or for where I can get the count and based on the count I can get the NodeName from the DATA.
This seems to do what you want:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my #nodes;
my $count = 0;
while (<DATA>) {
chomp;
push #nodes, $1 if /NodeName : (\w+)/;
$count = $1 if /Total number of data found :\s*(\d+)/;
}
print "Count:$count\n";
if ($count <= 0) {
print "Count is less than 1.. Exiting\n";
exit 0;
}
print Dumper(\#nodes);
__DATA__
Data : Network=R9,N=Node1
NodeName : Node1
DateTime : 05-08-2020 10:00:05
Data : Network=R9,N=Node2
NodeName : Node2
DateTime : 05-08-2020 10:00:10
Total number of data found : 2
Above solution is good. But if your data is in file (file operation) you can use https://metacpan.org/pod/File::Grep module
fmap BLOCK LIST
Performs a map operation on the files in LIST, using BLOCK as the mapping function. The results from BLOCK will be appended to the list that is returned at the end of the call.
use strict;
use warnings;
use File::Grep qw(fmap);
use Data::Dumper;
my #nodes;
my $count_flag = 0;
# my #result = fmap { <block> } file_name;
# modified "Total number .." regex, added \s*[1-9]\d*$
# (checking number between 1-9 followed by \d equivalent to [0-9])
# so if Total number of data found value is greater than zero setting flag to 1
fmap { (/NodeName : (\w+)/ ? push(#nodes,$1) : ((/Total number of data found :\s*[1-9]\d*$/) ? $count_flag = 1 : () ) )} *DATA;
if ($count_flag) {
print Dumper(\#nodes);
} else {
print "Count is less than 1.. Exiting\n";
exit 0;
}
__DATA__
Data : Network=R9,N=Node1
NodeName : Node1
DateTime : 05-08-2020 10:00:05
Data : Network=R9,N=Node2
NodeName : Node2
DateTime : 05-08-2020 10:00:10
Total number of data found : 1
Output
$VAR1 = [
'Node1',
'Node2'
];
Push elements to #nodes array in the first loop.
Use #nodes array after the loop only if $count is right.
You may clear #nodes array using #nodes = () ;.

How to provide default output

I'm trying to get the following Perl program to print a default value, if no result is returned from a query. I am not sure how to do that.
When I run the script, I get the error: Can't call method "country_name" on an undefined value. Are there Any ideas on how I can change the program to print a default value, if no results are returned?
#!/usr/bin/perl update IP addresses with country
use strict;
use warnings;
use Net::IPInfoDB;
my $psql = "/usr/local/pgsql/current/bin/psql";
my $db = 'cpi';
my $args = "-U postgres -qc";
my $date = `/bin/date +\%y\%m\%d%H`;
my $reportfile = "/tmp/multiiplogins-$date";
my $sendmail = "/usr/sbin/sendmail -t -fcpi\#user.com";
my $mailsubject = "Login Report";
my $mailto = 'email#user.com';
my $query = "SELECT userid, login, email, logins, ips FROM (SELECT userid,login,email, count(userid) AS logins, count(ipaddr) AS ips FROM (SELECT l.userid, u.login, u.email, l.ipaddr FROM synloginaccess l, synusers u$
my $query2 = "SELECT l.userid, login, email, ipaddr FROM synloginaccess l, synusers u where l.accesstime > (now() - interval '24 hours') and l.type=2 and l.userid=u.userid ORDER BY l.userid;";
open (REPORT, ">$reportfile");
my $command = qq/$psql $db $args "$query"/;
my $command2 = qq/$psql $db $args "$query2"/;
my $result = `$command`;
my $result2 = `$command2`;
my $g = Net::IPInfoDB->new;
$g->key("api_key");
#we split $login into an array, line-by-line
my #lines = split("\n", $result2);
for my $line (#lines) {
#now we iterate through every line one-by-one
if ($line =~ /(?<ip>\d+\.\d+\.\d+\.\d+)/) {
my $city = $g->get_city("$1");
my $addr = $g->get_country("$1");
print "$line " . "| " . "\t" . $city->city_name . ", " . $addr->country_name ."\n";
print REPORT "$line " . "| " . "\t" . $city->city_name . ", ". $addr->country_name ."\n";
}
else {
print "$line \n ";
}
}
close REPORT;
mailReport();
sub mailReport{
#mail it
open(MAIL, "|$sendmail");
print MAIL "To: $mailto\n";
print MAIL "Subject: $mailsubject\n";
print MAIL "\n";
open (INFILE, "$reportfile");
my #contents = <INFILE>;
my $line;`
$addr ? $addr->country_name : "default"

skipping a line in an array, Perl

I'm relatively new to Perl and I've come across this project that I'm having a bit of a hard time with.
The object of the project is to compare two csv files, one of which would contain:
$name, $model, $version
and the other which would contain:
$name2,$disk,$storage
in the end the RESULT file will contain that matched lines and put together the information like so:
$name, $model, $version, $disk,$storage.
I've managed to do this, but my problem is that when one of the elements in missing the program breaks. When it encounters a line in the file missing an element it stops at that line. How can I fix this problem? any suggestions or a way as to how I can perhaps make it skip that line and continue on?
Here's my code:
open( TESTING, '>testing.csv' ); # Names will be printed to this during testing. only .net ending names should appear
open( MISSING, '>Missing.csv' ); # Lines with missing name feilds will appear here.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#my (#array) =<FILE>;
my #hostname; #stores names
#close FILE;
#***** TESTING TO SEE IF ANY OF THE LISTED ITEMS BEGIN WITH A COMMA AND DO NOT HAVE A NAME.
#***** THESE OBJECTS ARE PLACED INTO THE MISSING ARRAY AND THEN PRINTED OUT IN A SEPERATE
#***** FILE.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if ( open( FILE, "file.txt" ) ) {
}
else {
die " Cannot open file 1!\n:$!";
}
$count = 0;
$x = 0;
while (<FILE>) {
( $name, $model, $version ) = split(","); #parsing
#print $name;
chomp( $name, $model, $version );
if ( ( $name =~ /^\s*$/ )
&& ( $model =~ /^\s*$/ )
&& ( $version =~ /^\s*$/ ) ) #if all of the fields are blank ( just a blank space)
{
#do nothing at all
}
elsif ( $name =~ /^\s*$/ ) { #if name is a blank
$name =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
#$hostname[$count]=$name;
#$count++;
}
elsif ( $model =~ /^\s*$/ ) { #if model is blank
$model =~ s/^\s*/missing/g;
print MISSING"$name,$model,$version\n";
}
elsif ( $version =~ /^\s*$/ ) { #if version is blank
$version =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
}
# Searches for .net to appear in field "$name" if match, it places it into hostname array.
if ( $name =~ /.net/ ) {
$hostname[$count] = $name;
$count++;
}
#searches for a comma in the name feild, puts that into an array and prints the line into the missing file.
#probably won't have to use this, as I've found a better method to test all of the feilds ( $name,$model,$version)
#and put those into the missing file. Hopefully it works.
#foreach $line (#array)
#{
#if($line =~ /^\,+/)
#{
#$line =~s/^\,*/missing,/g;
#$missing[$x]=$line;
#$x++;
#}
#}
}
close FILE;
for my $hostname (#hostname) {
print TESTING $hostname . "\n";
}
#for my $missing(#missing)
#{
# print MISSING $missing;
#}
if ( open( FILE2, "file2.txt" ) ) { #Run this if the open succeeds
#open outfile and print starting header
open( RESULT, '>resultfile.csv' );
print RESULT ("name,Model,version,Disk, storage\n");
}
else {
die " Cannot open file 2!\n:$!";
}
$count = 0;
while ( $hostname[$count] ne "" ) {
while (<FILE>) {
( $name, $model, $version ) = split(","); #parsing
#print $name,"\n";
if ( $name eq $hostname[$count] ) # I think this is the problem area.
{
print $name, "\n", $hostname[$count], "\n";
#print RESULT"$name,$model,$version,";
#open (FILE2,'C:\Users\hp-laptop\Desktop\file2.txt');
#test
if ( open( FILE2, "file2.txt" ) ) {
}
else {
die " Cannot open file 2!\n:$!";
}
while (<FILE2>) {
chomp;
( $name2, $Dcount, $vname ) = split(","); #parsing
if ( $name eq $name2 ) {
chomp($version);
print RESULT"$name,$model,$version,$Dcount,$vname\n";
}
}
}
$count++;
}
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if ( open( FILE, "file.txt" ) ) {
}
else {
die " Cannot open file 1!\n:$!";
}
}
close FILE;
close RESULT;
close FILE2;
I think you want next, which lets you finish the current iteration immediately and start the next one:
while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( $name && $model && $version );
...;
}
The condition that you use depends on what values you'll accept. In my examples, I'm assuming that all values need to true. If they need to just not be the empty string, maybe you check the length instead:
while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( length($name) && length($model) && length($version) );
...;
}
If you know how to validate each field, you might have subroutines for those:
while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( length($name) && is_valid_model($model) && length($version) );
...;
}
sub is_valid_model { ... }
Now you just need to decide how to integrate that into what you are already doing.
You should start by adding use strict and use warnings to the top of your program, and declaring all variables with my at their point of first use. That will reveal a lot of simple mistakes that are otherwise difficult to spot.
You should also use the three-parameter for of open and lexical filehandles, and the Perl idiom for checking exceptions on opening files is to add or die to an open call. if statements with an empty block for the success path waste space and become unreadable. An open call should look like this
open my $fh, '>', 'myfile' or die "Unable to open file: $!";
Finally, it is much safer to use a Perl module when you are handling CSV files as there are a lot of pitfalls in using a simple split /,/. The Text::CSV module has done all the work for you and is available on CPAN.
You problem is that, having read to the end of the first file, you don't rewind or reopen it before reading from the same handle again in the second nested loop. That means no more data will be read from that file and the program will behave as if it is empty.
It is a bad strategy to read through the same file hundreds of times just to pair up coresponding records. If file is of a reasonable size you should build a data structure in memory to hold the information. A Perl hash is ideal as it allows you to look up the data corresponding to a given name instantly.
I have written a revision of your code that demonstrates these points. It would be awkward for me to test the code as I have no sample data, but if you continue to have problems please let us know.
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new;
my %data;
# Read the name, model and version from the first file. Write any records
# that don't have the full three fields to the "MISSING" file
#
open my $f1, '<', 'file.txt' or die qq(Cannot open file 1: $!);
open my $missing, '>', 'Missing.csv'
or die qq(Unable to open "MISSING" file for output: $!);
# Lines with missing name fields will appear here.
while ( my $line = csv->getline($f1) ) {
my $name = $line->[0];
if (grep $_, #$line < 3) {
$csv->print($missing, $line);
}
else {
$data{$name} = $line if $name =~ /\.net$/i;
}
}
close $missing;
# Put a list of .net names found into the testing file
#
open my $testing, '>', 'testing.csv'
or die qq(Unable to open "TESTING" file for output: $!);
# Names will be printed to this during testing. Only ".net" ending names should appear
print $testing "$_\n" for sort keys %data;
close $testing;
# Read the name, disk and storage from the second file and check that the line
# contains all three fields. Remove the name field from the start and append
# to the data record with the matching name if it exists.
#
open my $f2, '<', 'file2.txt' or die qq(Cannot open file 2: $!);
while ( my $line = $csv->getline($f2) ) {
next unless grep $_, #$line >= 3;
my $name = shift #$line;
next unless $name =~ /\.net$/i;
my $record = $data{$name};
push #$record, #$line if $record;
}
# Print the completed hash. Send each record to the result output if it
# has the required five fields
#
open my $result, '>', 'resultfile.csv' or die qq(Cannot open results file: $!);
$csv->print($result, qw( name Model version Disk storage ));
for my $name (sort keys %data) {
my $line = $data{$name};
if (grep $_, #$line >= 5) {
$csv->print($result, $data{$name});
}
}

Who user only DB class from PERA?

I want to use only PEAR DB class, I try follow:
$pearPfad ="PEAR_DB-1.7.14/DB-1.7.14/DB.php";
error_reporting(E_ALL);
require_once($pearPfad);
$DB_dbType ="mysql";
$DB_user = "myuser";
$DB_pass = "mypassword";
$DB_host = "localhost";
$DB_dbName ="mydatabase";
$dsn = $DB_dbType . "://" // Build a DSN string (Data Source Name)
. $DB_user . ":" // Required by DB::connect()
. $DB_pass . "#"
. $DB_host . "/"
. $DB_dbName;
$db = DB::connect($dsn, TRUE);
if (DB::isError($db)) {
die("FEHLER: ".$db->getMessage() );
}
$sql = "SELECT id, title , created FROM h1z4e_content";
$res = $db->query($sql);
if (DB::isError($res)) {
die($res->getMessage());
}
while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
if (DB::isError($row)) {
die($row->getMessage());
}
print ("<p>Hey, it s:<br />" . $row->id . " " . $row->title . " ... and their freaky et: " . $row->created. "</p>\n");
}
$res->free();
// Similar to: mysql_free_resul
$db->disconnect();
But I get this error:
Warning: require_once(PEAR.php): failed to open stream: No such file or directory in /var/www/PEAR/PEAR_DB-1.7.14/DB-1.7.14/DB.php on line 30 Fatal error: require_once(): Failed opening required 'PEAR.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/PEAR/PEAR_DB-1.7.14/DB-1.7.14/DB.php on line 30
I found here in Stack Overflow this link: PHP-PEAR require_once('DB.php');
I don't want install PEAR on my computer.
I want only user PEAR DB class, is this possible without install PEAR?
Yes, but you need to set the include path correctly - in your case, to /full/path/to/PEAR_DB-1.7.14/DB-1.7.14/

How to send email after registering

After that someone registers in a site, a mail is usually sent to his mail account. But to generate this link or what info can be placed in this link so that it can be used to activate the user account??
you can place any thing which can identify a valid user
1- A Hash Value
2- An Encrypted String
3- A Guid
and when user clicks on the link , you can validate the value.
Check this part of code:
Generate code and e-mail:
/* if $acces = 0 everything is perfect so the system send a confirmation mail */
if($acces == 0)
{
print("<br>A mail has been send to " . $mail . "<br><br>") ;
/* prepare the vars */
$activ = $user . $pass ;
$code = md5($activ) ;
/* to how send to mail */
$to = $mail ;
/* prepare the subject */
$subject = "You need to confirm you registration to " . $_SERVER['HTTP_HOST'] ;
/* start writing the message */
$message = "Hello " . $user . ",\r\n\r\n" ;
$message .= "Thank you for registering at " . $_SERVER['HTTP_HOST'] . " Your account is created and must be activated before you can use it.\r\n" ;
$message .= "To activate the account click on the following link or copy-paste it in your browser :\r\n\r\n" ;
$message .= "http://" . $_SERVER['HTTP_HOST'] . "/~carron/registration/register_send.php?user=" . $user . "&activation=" . $code . "\r\n\r\n" ;
$message .= "After activation you may login to http://" . $_SERVER['HTTP_HOST'] . " using the following username and password:\r\n\r\n" ;
$message .= "Username - " . $user . "\r\nPassword - " . $pass . "\r\n" ;
/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";
/* set up additional headers */
$headers .= "To: " . $to . "<br>\n" ;
$headers .= "From: " . $from . $addmail ;
/* writing data in the base */
$query = "INSERT INTO registration (user, pass, activ, mail) VALUES ('$user', '$pass', '$code', '$mail') ;" ;
$result = mysql_query($query, $db);
if ($result == false)
die("Failed " . $query);
else
{
/* everything went well so we can mail it now */
mail($to, $subject, $message, $headers);
}
}
Check activation:
/* controle if the validation link is right */
$x = 0 ;
$query = "SELECT user, pass, activ, mail FROM registration WHERE user = '" . $username . "';" ;
$result = mysql_query($query, $db);
if ($result == false) die("Failed " . $query);
while ($fields = mysql_fetch_row($result))
{
for ($i=0, $max=sizeof($fields) ; $i < $max ; $i++)
{
$tmp[$i] = $fields[$i] ;
}
/* the activation link is right so we can update
the datas in the data base */
if($activation == $tmp[2] AND $username == $tmp[0])
{
$x = 1 ;
$query2 = "UPDATE registration SET activated = '1' WHERE user = '" . $username . "' AND activ = '" . $activation . "' ;" ;
$result2 = mysql_query($query2, $db);
if ($result2 == false)
die("Failed " . $query2);
}
else
$x = -1 ;
}
/* give a confirmation message to the user */
if($x == 1)
print($username . " your activation has been done perfectly<br> Thank you...") ;
else
print($username . " your activation has not been done corectly<br> Please try again later...") ;
Script from PHPclasses.org
The idea is to have a link that only the recipient of the email knows. So when that link is visited on your site, you know that someone has read the email you sent and clicked on the link and so you can presume that the person who registered and the person who read the email are the same.
As such, you just need a link that can't be easily guessed. Pick something random (and record it in the user's profile) or hash the user name + a seed, or something.
When user registered, you can use uniqid() to create an activate code and stored in database. Then in mail, give a link like: http://....../activate.php?code=[uniqid()]
In activate.php, you can read activate code from database and check it.

Resources