Easy question about strlen ()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Easy question about strlen ()

Post by Chris Corbyn »

Hi,

I want to make sure the length of a string a user inputs in a form matches the length of a substring of a variable I've set.

How do I make sure both strings are of equal length? i've tried playing around by doing this:

Code: Select all

if (strpos(strlen($_POST['auth']), strlen(substr($_POST['code'], 0, -4)) !== false) {
But it just says parse error for that line of code.

Thanks Guys :)
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

Hello,

do i understabd you idea right userstringlenght == variablelenght?

then use

Code: Select all

<?php
if (strlen($_POST['auth'])== strlen($_POST['code']))
{

}
?>
greez Pozor
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

no pozor youdon't understand his question. He is trying to match user input with a substring of anoyther variable. Even if taht was correct your missing is a ). it should look mroe like this:

Code: Select all

if (strpos(strlen($_POST['auth']), strlen(substr($_POST['code'], 0, -4))) !== false) {
but tahts not right. strpod() takes in strings not integer. So im not quite sure i understand waht your trying to accomplish here
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes that's what I wanted to do but I only wanted to check everything but the last four characters in the $_POST['code'] variable using substr($_POST['code'], 0, -4) and it didn't work combining that with strlen if you get where I'm coming from?

I've found a way around it by just adding the line $codelength = substr($_POST['code'], 0, -4) and then using

Code: Select all

if (strlen($_POST['auth']== strlen($codelength)) {
and this worked.

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

sorry just noticed I've missed a bracket out in the reply above.

Code: Select all

$codelength = substr($_POST['code'], 0, -4);

Code: Select all

if (strlen($_POST['auth'])== strlen($codelength)) {
That's how I did it
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

why are you checking the length?? i dont understand why your checking the length that desn't make any sense. And if you know that the length of the second string is always going to be four then why dont you check to see that the auth is 4 chars
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

That is a mess.

I recommend putting the int values returned into a variable and then placing them inside of another function.

You can spot parse errors much easier that way, and it is a bit less confusing at first glance.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's ok I've got it sorted now anyway thanks.

illusionist... I'll try to put your mind at rest about what I was doing.

I have a page which loads a random image (showing 6 digits) and a form with a submit button.

The filename of the image is identical to the 6 digit number it contains.

The user is required to input the correct 6 digits in order to progress to the next page. This worked fine simply by checking the filename (minus the extension) against the users input. BUT...

I was using strpos() to do that bit and it still came out as a correct entry if extra digits were added in the user input. Not a big problem but I just wanted it work perfectly so I added and extra if () check to make sure the length matched too.

Here's the code on both pages (it's for downloading polyphonic ringtones in case you wondered).
This is the page with the random image and form

Code: Select all

<?php

/*Creates the variable $file from the GET info on the url of the download link*/

$file=$_GET['file'];
if (empty($file)) {
$file='index';
}

/*Function to get array of all images in directory*/

function listdir()
{
	$ImgArray = array();

/*Specifies the directory files are in*/
        
	$handle = opendir('img');

/*Loops in directory to find files*/

	while ($file = readdir($handle))
	{

/*Reads the file extensions*/

		$imgfile = substr($file, -3);

		if(($imgfile == 'gif') || ($imgfile == 'jpg') || ($imgfile == 'png') )
		{
			$ImgArray[] = $file;
		}
	}

	return $ImgArray;
}

/*Shuffles the array*/

$ImgArray = listdir();
shuffle ($ImgArray);

echo '<p align="center">';
	echo '<img src="download.gif">';
echo '</p>';
echo '<p class="test">Please enter the code shown in the box below to<br>';
echo 'download <b>';
echo $_GET['n'];

echo '</b>:';
echo '<p align="center">';
	echo '<table width="160" border="0">';
	echo '<tr>';
		echo '<td align="Right">';

/*Displays first image in the shuffled array*/

		echo '<img src="img/' . $ImgArray[0] . '">';

		echo '<p>';
		echo '<form action="download.php" method="post" target="_new">';
		echo '<input type="text" name="auth">';

/*Link information and image name are posted in form*/

		echo "<input type='hidden' value='" . htmlspecialchars($ImgArray[0]) . "' name='code' />\n";
		echo "<input type='hidden' value='" . htmlspecialchars($_GET['maintype']) . "' name='maintype' />\n";
		echo "<input type='hidden' value='" . htmlspecialchars($_GET['n']) . "' name='filename' />\n";
		echo '<p>';
		echo '<input type="submit" name="submit" value="Download">';
		echo '</form>';
		echo '</td>';
	echo '</tr>';
	echo '</table>';
?>

(Having problems? <a href="index.php?location=help">Click Here</a>.)
And this is the the page which redirects to the download if the input was correct. It'll be scrappy but I've only just started learning PHP so hey, at least it works :-)

Code: Select all

<?php

$nokstring = 'sorry i've hidden this path because i made the script for a reason';
$codelength = substr($_POST['code'], 0, -4);

if (strpos($_POST['auth'], substr($_POST['code'], 0, -4)) !== false) {
	if (strlen($_POST['auth'])== strlen($codelength)) {
		if (strpos($_POST['maintype'], 'nokia') !== false) {
			header ('Content-type: Audio/Midi');
			header ('Content-Disposition: attachment; filename="'.$_POST['filename'].'.mid"');
			readfile (''.$nokstring.$_POST['filename'].'.mid');
		}
	} else {
	echo 'Sorry wrong code <p>';
	echo 'Please <a href="javascript:window.close();">click here</a> to close this window and try again';
	}
} else {
	echo 'Sorry wrong code <p>';
	echo 'Please <a href="javascript:window.close();">click here</a> to close this window and try again';
}

?>
The variables not defined in these pages have been passed around from previous pages.

Hope this helps you understand what I was doing
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ok, so say the filename was like 123456.img then they would input 123456, right? So you before were checking it, and even if they input more than 6 digits it owuld say it was right? did you try simply:

Code: Select all

$auth = $_POST['auth'];
$filename = "123456.img"; // this would be what ever the file name is
//and if you don't knwo waht teh extension is goig to be you can do this:
$path_info = pathinfo($filename);
$file = basename($filename, $path_info['extension']) //will return 123456
if ($auth == $file){
//do whatever
}else{
//wrong auth
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ok thanks. No I never tried doing it like that. Makes more sense I guess. Us newbies hey.... Don't we make things more complicated than they need to be. I guess I'll learn with a bit of practice :-)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol, comparing two values to see if they are the same is always the easiest way!!
Post Reply