TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-22-2008, 07:22 PM   #1 (permalink)
The Wanderer
Newcomer 
 
Join Date: Feb 2008
Posts: 6
Thanks: 2
pabin is on a distinguished road
Plugin/Addon String help!

Well i have been trying to extract each chars of a string and keep them in an array. I have used loop to get it but failed. any ideas...
e.g.
Code:
$string = 'hello';
$len = strlen($string);
for($i = 0; $i < $len; $i++){
$char = $string{$i};
//now i am stuck
}
pabin is offline  
Reply With Quote
Old 02-22-2008, 07:52 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

You would want to do something like this:
PHP Code:
$arr_char = array();
$string 'hello';
$len strlen($string);

for(
$i 0$i $len$i++)
{
    
$arr_char[] = substr($string$i1);

$arr_char will look like this:
PHP Code:
Array
(
    [
0] => h
    
[1] => e
    
[2] => l
    
[3] => l
    
[4] => o

__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 02-22-2008, 07:53 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

BTW... welcome to the forums!
You should go introduce yourself over in the new member area.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 02-22-2008, 07:54 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

You can think about this two ways.

First, you can access the string as if it were an array (which you're attempting to do), e.g.
PHP Code:
$string'hello';
// display the first character in the string, at index 0
echo $string[0]; // 'h' 
In your loop, you would refer to it with the [ ] brackets, not the { } braces. I haven't tested it, but I believe that actually tacks the numeric value of $i to the end of $string at that point in the loop. The other important factor, $char is not defined as an array. It's just a string as well, which is reassigned each time through the loop. I think you want something more like this
PHP Code:
// our string
$string'hello';
// an empty array
$charArray= array();
for ( 
$i=0$istrlen($string); $i++ ) {
  
// assign each character in the string to an empty index
  
$charArray[]= $string[$i];
}
// display the array values
print_r($charArray); 
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-22-2008, 08:36 PM   #5 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

In php5 you can say:

PHP Code:
$char_list str_split('TalkPHP');
/**
$char_list = Array('T', 'a', 'l', 'k', 'P', 'H', 'P');
*/ 
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following 3 Users Say Thank You to Kalle For This Useful Post:
Alan @ CIT (02-22-2008), pabin (02-23-2008), ReSpawN (02-23-2008)
Old 02-23-2008, 12:18 AM   #6 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Strings are arrays by default.. you can access each letter directly as SOCK showed.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 02-23-2008, 06:44 AM   #7 (permalink)
The Wanderer
Newcomer 
 
Join Date: Feb 2008
Posts: 6
Thanks: 2
pabin is on a distinguished road
Default

Thanks for your help. Actually i was trying to make script which would be used for authentication. I thought i would count the length of the password and get each char from password and hash them differently and merge them at end before sending it to the db for authenticaion.
pabin is offline  
Reply With Quote
Old 02-23-2008, 09:02 AM   #8 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

That'll be quite a waste of good resources. Encoding a password which has about 15 letters, would take a lot longer. Than you'll do something like;

PHP Code:
<?php

$stringArray 
= array();
$input 'thisWillbeMyPass';
$length strlen($input);

for(
$i 0$i $length$i++) {
    
$stringArray[] = $input[$i];
}
echo 
'You\'ve stripped '.$i.' words and put them into the array.';
echo 
'<br><b>Total Length:</b> '.$length.' :: <b>Total Stripped:</b> '.$i;

echo 
'<br><br>Your '.$i.' long string put together: ';
for (
$i 0$i $length$i++) {
    echo 
$stringArray[$i];
}

echo 
'<br><br><b>The encrypted one:</b> <br>';
for (
$i 0$i $length$i++) {
    echo 
md5($stringArray[$i]).'&nbsp;&nbsp;&nbsp;['.$stringArray[$i].']<br>';
}

echo 
'<br><br>And of course the total encrypted one: ';
$encrypted '';
for (
$i 0$i $length$i++) {
    
$encrypted .= md5($stringArray[$i]);
}
echo 
'<br><br>Slow, long one: '.md5($encrypted).'<br>Faster but more hackable one: '.md5($input);
?>
Which will output:
Quote:
You've stripped 16 words and put them into the array.
Total Length: 16 :: Total Stripped: 16

Your 16 long string put together: thisWillbeMyPass

The encrypted one:
e358efa489f58062f10dd7316b65649e [t]
2510c39011c5be704182423e3a695e91 [h]
865c0c0b4ab0e063e5caa3387c1a8741 [i]
03c7c0ace395d80182db07ae2c30f034 [s]
61e9c06ea9a85a5088a499df6458d276 [W]
865c0c0b4ab0e063e5caa3387c1a8741 [i]
2db95e8e1a9267b7a1188556b2013b33 [l]
2db95e8e1a9267b7a1188556b2013b33 [l]
92eb5ffee6ae2fec3ad71c777531578f [b]
e1671797c52e15f763380b45e841ec32 [e]
69691c7bdcc3ce6d5d8a1361f22d04ac [M]
415290769594460e2e485922904f345d [y]
44c29edb103a2872f519ad0c9a0fdaaa [P]
0cc175b9c0f1b6a831c399e269772661 [a]
03c7c0ace395d80182db07ae2c30f034 [s]
03c7c0ace395d80182db07ae2c30f034 [s]


And of course the total encrypted one:

Slow, long one: f071e1193cfd4c99d92e1292a70d8f83
Faster but more hackable one: eba11541832ba056a064476ce09afbad
I think that is something you wanted? There surely are easier ways, like using SALT or triple encodes with a mixture of SHA1 and MD5.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following User Says Thank You to ReSpawN For This Useful Post:
pabin (02-24-2008)
Old 02-23-2008, 10:26 AM   #9 (permalink)
The Wanderer
Newcomer 
 
Join Date: Feb 2008
Posts: 6
Thanks: 2
pabin is on a distinguished road
Default

Actually i was trying to do the same way but later figured out that this would take longer time if password is long. Well i have been mixing password with salt and hashing with md5 and sha couple of time and reversing the hash or changing the pos of 16 chars of first and last chars of MD5 then only checking with the DB.
pabin is offline  
Reply With Quote
Old 02-23-2008, 10:32 AM   #10 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by pabin View Post
Actually i was trying to do the same way but later figured out that this would take longer time if password is long. Well i have been mixing password with salt and hashing with md5 and sha couple of time and reversing the hash or changing the pos of 16 chars of first and last chars of MD5 then only checking with the DB.
Also a pretty effective way. Reversing the string has never popped into my thoughts at all. Great idea, good luck non the less.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:33 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design