Forum Moderators: coopster
First Last^login^street^city^state^zip^password~
Here is my code so far:
<?php
$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));
$my_users = preg_split('/~/', $user_file, -1);
foreach ($my_users as $textline) {
echo "TEXTLINE - $texline <BR>";
list($name, $login, $street, $city, $state, $zip, $password) = split('/^/',$textline);
echo "HERE TIS $name <BR>";
//$name is showing full $TEXTLINE, doesn't appear to be splitting
}
//if ($login == "") {
//header('Location: login.php');
//} else {
//setcookie ('verify', $login, 0);
//header('Location: shop.php');
//}
?>
Right now $name is returing the entire textline. It's not splitting between entries. Here is the output:
TEXTLINE -
HERE TIS Rob Cochran^rcochran^#*$! #*$!xxxx St.^xxxxxxxxx^CO^11111^passwordTEXTLINE -
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaunTEXTLINE -
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreatTEXTLINE -
HERE TIS
Any ideas? I am new to PHP, have used Perl but obviously the Split is a little different.
Thanks!
TEXTLINE -
HERE TIS Rob Cochran^rcochran^6710 #*$! xxx xxxx.^xxxxxxx^xx^xxxx^password
TEXTLINE -
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun
TEXTLINE -
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat
TEXTLINE -
HERE TIS
Code:
<?php
$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));
$my_users = preg_split('/~/', $user_file, -1);
foreach ($my_users as $textline) {
echo "TEXTLINE - $texline <BR>";
$meminfo = split('/^/',$textline);
echo "HERE TIS $meminfo[0] <BR>";
//$name is showing full $TEXTLINE, doesn't appear to be splitting
}
//if ($login == "") {
//header('Location: login.php');
//} else {
//setcookie ('verify', $login, 0);
//header('Location: shop.php');
//}
?>
This is really wierd. Thanks anyway for the suggestion. Any more?
Thanks again,
Rob
I'll check back for any more ideas!
Rob
HERE TIS Rob Cochran^rcochran^#*$! xxx xxxx.^xxxxxx^xx^8xxxxpassword
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat
<?
$my_users = array('Rob Cochran^rcochran^#*$! #*$! xxxx.^xxxxxx^xx^8xxxxpassword',
'Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun',
'Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat');
foreach ($my_users as $textline) {
echo "<p>TEXTLINE -<BR>";
$meminfo = explode('^',$textline);
echo '<pre>';
print_r($meminfo);
echo '</pre>';
}
?>
To search for the literal value '^', you'll need to escape it with a backslash.
Make the relevant line look like this:
$meminfo = split('/\^/',$textline);
Here is the winning code:
<?php
$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));
$my_users = preg_split('/~/', $user_file, -1);
foreach ($my_users as $textline) {
$meminfo = explode('^',$textline);
if ($meminfo[1] == $login) {
if ($meminfo[6] == $passwd) {
echo "WE HAVE A WINNER! " . $meminfo[1] . " " . $meminfo[6];
}
}
}
?>
Rob
this
if ($meminfo[1] == $login) {
if ($meminfo[6] == $passwd) {
echo "WE HAVE A WINNER! " . $meminfo[1] . " " . $meminfo[6];
}
}
turns into
if ($meminfo[1] == $login && $meminfo[6] == $passwd) echo 'WE HAVE A WINNER! ',$meminfo[1],' ',$meminfo[6];
inline single if
single quotes since nothing needs to be parsed for vars
commas are supported in echo so then it doesn't have to concatenate first and then echo, it does it faster
;)