I got this Undefined Offset error from PHP, and I can't seem to see why. Can you guys help me out?
I'm pretty new to PHP, so I don't really understand what Undefined offset really is.
<?php
$wordlist = file('/tmp/all'); // 42M wordlist
$user = "admin";
$realm = "Miele Logic";
$nonce = "07ec2416ef0000009223000015000000";
$nc = "00000001";
$cnonce = "gdBxXVNT0y6npOpQ";
$qop = "auth";
$i = 0;
while($wordlist[$i]) {
$password = trim($wordlist[$i]);
$HA1 = md5("$user:$realm:$password");
$HA2 = md5("GET:/");
$response = md5("$HA1:$nonce:$nc:$cnonce:$qop:$HA2");
if($i % 100000 == 0) {
echo "$i\n"; // output $i at each 100k
}
if($response == '97b5e79866512f028266f34946117a2c') {
echo $password . "n";
break;
}
$i++;
}
?>
Change your code to:
$i = 0;
$count = count($wordlist);
while($i < $count) {
//rest of the code goes here
}
Related
I'm running a simple script which puts an integer through the formula of the Collatz conjecture and adds the output of each step into an array.
I want to use a function to detect if there's a cycle in the array, using Floyd's algorithm. And though I feel like I'm not doing a bad job, I don't seem to get it right. At this moment I'm getting the error Trying to get property 'next' of non-object in C:\xampp\htdocs\educom\week3\functions.php on line 12
See my code below. Any feedback is greatly appreciated!
include("functions.php");
$n = $_POST['number'];
$step = 0;
$reeks1 = array();
$cycle = 0;
echo "Your entry is: ". $n ."<br><br>";
while($n!==1 && $cycle==0){
$cycle = detect_cycle(array($reeks1));
if($n % 2 == 0){
$n = $n / 2;
array_push($reeks1, "$n");
$step++;
echo $step .": ". $n ."<br>";
}else{
$n = ($n * 3) + 1;
array_push($reeks1, "$n");
$step++;
echo $step .": ". $n ."<br>";
}
}
functions.php:
function detect_cycle($node){
if ($node==NULL){
return FALSE;
}
$turtle = $node;
$rabbit = $node->next;
while($rabbit != NULL){
if($rabbit === $turtle){
return TRUE;
}elseif($rabbit->next == NULL){
return FALSE;
}else{
$turtle = $turtle->next;
$rabbit = $rabbit->next->next;
}
}
return FALSE;
}
Check this out. IMPORTANT I don't know is this according to your theory. but it won't give you errors if you use like this.
function detect_cycle($node){
if ($node==NULL){
return FALSE;
}
$turtle = $node;
$rabbit = $node[0];
while($rabbit != NULL){
if($rabbit === $turtle){
return TRUE;
}elseif($rabbit[0] == NULL){
return FALSE;
}else{
$turtle = $turtle[0]; // use the number of the element key starting from 0
$rabbit = $rabbit[0][1];
}
}
return FALSE;
}
Getting Notice: Undefined offset: 25 in
C:\wamp\www\finalProjectDemo\search.php on line 32
I'm trying to read in from a file and search for a specific name and address within that for output. I know a database would be best. This is for a class assignment I'm giving out that's specifically set to work this way. I believe I almost have it all, but am just getting this problem. Fairly new to PHP.
I have this code:
<html>
<body>
<?php
// read lines into array
// search array for string
// get 7 lines from there.
$i = 0;
$fileName = "addresses.txt";
$readFile = fopen($fileName, 'r');
$readByLineArray = array();
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
while (!feof($readFile))
{
$readByLineArray[$i] = fgets($readFile);
//echo "$readByLineArray[$i] read from array position $i";
//echo "<br />";
$i++;
}
fclose($readFile);
$arrLength = count($readByLineArray);
$currentArrayPosition = 0;
for ($x=0;$x<=$arrLength;$x++){
if ($searchFirstName == $readByLineArray[$x])
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $readByLineArray[$x])
{
$searchLastNameSuccess = 1;
$currentArrayPosition = $x - 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
for ($y=0;$y<=7;$y++){
echo "$readByLineArray[$currentArrayPosition]<br />";
$currentArrayPosition++;
}
?>
</body>
</html>
Thanks for all your help!
Ben---
Try foreach :-
foreach ($readByLineArray as $temp){
if ($searchFirstName == $temp)
{
$searchFirstNameSuccess = 1;
$x++;
if ($searchLastName == $temp)
{
$searchLastNameSuccess = 1;
} else {
$searchFirstNameSuccess = 0;
}
}
}
Change your for loop like this..
for ($x=0;$x<$arrLength;$x++){ //<--- Should be < and not <=
Say if your array count is 3 , so the array elements keys are arranged as 0,1,2. When you put <= in the looping as condition , your code will check for an non-existent key with an index of 3 which will thrown an Undefined Offset notice.
EDIT :
The easier way....
<html>
<body>
<?php
$fileName = "addresses.txt";
// Get search string from submission
$searchFirstName = $_POST['searchFirstName'];
$searchLastName = $_POST['searchLastName'];
$searchFirstNameSuccess = 0;
$searchLastNameSuccess = 0;
foreach(file($fileName) as $recno=>$records)
{
if(stripos($records,$searchFirstName)!==false && stripos($records,$searchLastName)!==false)
{
$searchFirstNameSuccess = 1;
$searchLastNameSuccess = 1;
echo "Match Found at Position : $recno";
break;
}
}
?>
</body>
</html>
I am using a for loop on my client's website to insert purchased ticket information into a database. The loop is working correctly, but the client has requested the option to attach a unique identifier to the first entry every time the for loop is run. This identifier would be used to highlight the primary ticket owner when the tickets are printed. I have included the current for loop below for reference. Any ideas on how to achieve this? Thank you.
$threepack = '';
$i = '';
for($i = 0; $i < $tickets; $i++)
{
$firstname = 'firstname'.$threepack;
$lastname = 'lastname'.$threepack;
$address = 'address'.$threepack;
$city = 'city'.$threepack;
$postal = 'postal'.$threepack;
$phone = 'phone'.$threepack;
$altphone = 'altphone'.$threepack;
$sec_firstname = 'sec_firstname'.$threepack;
$sec_lastname = 'sec_lastname'.$threepack;
$email = 'email'.$threepack;
$table->firstname = $data->$firstname;
$table->lastname = $data->$lastname;
$table->address = $data->$address;
$table->city = $data->$city;
$table->postal = $data->$postal;
$table->phone = $data->$phone;
$table->altphone = $data->$altphone;
$table->sec_firstname = $data->$sec_firstname;
$table->sec_lastname = $data->$sec_lastname;
$table->email = $data->$email;
$table->id = 0;
$table->order_total = $data->total;
$table->store();
if($data->tickets == '-1')
{
if($threepack == 2)
{
$threepack = 3;
} else {
$threepack = 2;
}
}
// 8 Fields
if($data->tickets == '5')
{
if ($threepack == '') {
$threepack = 2;
} else {
$threepack += 1;
}
}
}
for ($i = 0; $i < $tickets; $i++) {
// ...
if ($i == 0)
$table->highlightme = 1;
// ...
$table->store();
// ...
}
I have that code :
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$wynik = 0;
$licznik = 0;
wykonaj();
function wykonaj()
{
echo '1';
for($i = 1; $i == 404; $i++)
{
$temp = $i / 100;
echo $temp;
if(is_int($temp*1.5))
{
global $wynik, $licznik;
$wynik++;
//$liczba[$licznik] = $i + ($i/100)*1.5;
$licznik++;
}
}
}
echo "Ilosc wynikow : $wynik<br/>Liczby sa nastepujace :";
?>
My application returns :
1Ilosc wynikow : 0
Liczby sa nastepujace :
So I think there is an error (logical ?), because that line - echo $temp; isn't displayed yet, but 1 before a loop is displayed. I tried a lot of things, but I could not find any solution. Why are my errors ? Compiler doesn't tell anything.
It will only enter the loop if $i is equal to 404, which it's set to 1 right off so that won't happen.
Syntax in a for loop is something like:
for([Set Vars]; [Set Conditional]; [Set Increment])
The loop only runs when the conditional is true.
Try this code. Mising <= in the for condition
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$wynik = 0;
$licznik = 0;
wykonaj();
function wykonaj()
{
echo '1';
for($i = 1; $i <= 404; $i++)
{
$temp = $i / 100;
echo $temp;
if(is_int($temp*1.5))
{
global $wynik, $licznik;
$wynik++;
//$liczba[$licznik] = $i + ($i/100)*1.5;
$licznik++;
}
}
}
echo "Ilosc wynikow : $wynik<br/>Liczby sa nastepujace :";
?>
demo here
Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.