I am building a script thats goal is to check up to 100 URLS for validity (No 404).
The only variable in the URL is the page number, like so:
http://example.com/category/id/products/page/1
http://example.com/category/id/products/page/2
and so on up to 100,
as soon as my code reaches an invalid URL, I want it to stop and echo the number it has reached, this is the code I am trying to no avail:
$url ="http://example.com/category/id/products/page/1";
if (false !== strpos($url, $id)) {
$pageNumber = 2;
$check = true;
do{
$urlIterate = "http://example.com/category/id/products/page/".$pageNumber;
if(false !== strpos($urlIterate, $id)){
$pageNumber++;
}
else{
$check = false;
}
}
while($pageNumber <= 99);
}
else{
$check = false;
echo 'No pages were found at all';
}
echo "There were ". $pageNumber." pages.;
?>
Im not sure if this is what youre looking for, but try this:
<?php
$id_to_search = "90";
for ($i = 1; $i <= 100; $i++) {
$url = "http://example.com/category/id/products/page/" . $i;
$values = parse_url($url);
$paths = explode('/', $values['path']);
$id_from_url = $paths[5];
if ($id_to_search === $id_from_url) {
$headers = get_headers($url);
if ($headers[0] == 'HTTP/1.0 404 Not Found') {
echo "URL Found! URL is invalid(404). URLs searched = " . $i . "<br>";
} else {
echo "URL is valid<br>";
}
} else {
echo "URL was searched but it does not match the ID we are looking for<br>";
}
}
Why are you not using the for loop? It will be better while we know how much iterations will we need.
for($i = 1; $1<=100; $i++){
$urlIterate = "http://example.com/category/id/products/page/".$i; //generate url
$headers = get_headers($urlIterate, 1); //get headers
if($headers[0] != 'HTTP/1.1 200 OK'){ //if we have an error
if($i > 1) //if there was at least one found
echo 'Last found number is ' . ($i-1);
else
echo 'No pages were found at all';
break; //stops the 'for' loop
}
}
Your code is looking for $id in urls - what's the point?
Related
I am working on an android app that should create some records on a MySQL database.
This is the PHP file that receives the POST values from the app.
As you may see, there are three arrays that collect specific POST.
The first and second loops are working fine and executing the related guardar_post_media() function.
But the third loop is not executed and there are real values on the variables inside the third array.
May be there is something wrong that I canĀ“t detect, may be you can.
<?php
require_once '../mis_php_functions/funciones_basicas.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$val39 = $_POST['val39'];
$val40 = $_POST['val40'];
$val46 = $_POST['val46'];
$val48 = $_POST['val48'];
$val50 = $_POST['val50'];
$val52 = $_POST['val52'];
$val54 = $_POST['val54'];
$val56 = $_POST['val56'];
$val58 = $_POST['val58'];
$val60 = $_POST['val60'];
$val62 = $_POST['val62'];
$val64 = $_POST['val64'];
$val65 = $_POST['val65'];
$val67 = $_POST['val67'];
$val69 = $_POST['val69'];
$val71 = $_POST['val71'];
$val73 = $_POST['val73'];
$val75 = $_POST['val75'];
$val77 = $_POST['val77'];
$val79 = $_POST['val79'];
$val81 = $_POST['val81'];
$val82 = $_POST['val82'];
$val83 = $_POST['val83'];
$val84 = $_POST['val84'];
$val85 = $_POST['val85'];
$val86 = $_POST['val86'];
$val87 = $_POST['val87'];
$val88 = $_POST['val88'];
$val89 = $_POST['val89'];
$val100 = $_POST['val100'];
$val101 = $_POST['val101'];
$val102 = $_POST['val102'];
$val103 = $_POST['val103'];
$val104 = $_POST['val104'];
$status = 1;
$post = guardar_post($val40,$val39,$val100,$val102,$status,$val103);
if ($post != false) {
$fotos = array($val48,$val50,$val52,$val54,$val56,$val58,$val60,$val62,$val64);
$arrayLength = count($fotos);
echo "Numero de fotos ".$arrayLength;
$i = 0;
while ($i < $arrayLength)
{
if ($fotos[$i] == 0){
}
else{
guardar_post_media(1,$fotos[$i],$val102,$val100,$post);
}
echo "<br />".$fotos[$i] ."<br />";
$i++;
}
$videos = array($val67,$val69,$val71,$val73,$val75,$val77,$val79,$val81,$val83);
$arrayLength2 = count($videos);
echo "Numero de videos ".$arrayLength2;
$i = 0;
while ($i < $arrayLength2)
{
if ($videos[$i] == 0){
}
else{
guardar_post_media(2,$videos[$i],$val102,$val100,$post);
}
echo "<br />".$videos[$i] ."<br />";
$i++;
}
$youtube = array($val85,$val86,$val87,$val88,$val89);
$arrayLength3 = count($youtube);
echo "Numero de youtube ".$arrayLength3;
$i = 0;
while ($i < $arrayLength3)
{
if ($youtube[$i] == 0){
}
else{
guardar_post_media(3,$youtube[$i],$val102,$val100,$post);
}
echo "<br />".$youtube[$i] ."<br />";
$i++;
}
sendMessageNuevoPost($val39,$val102,$val103,$val104); // envio de push
}
else{
echo 'error';
}
}
?>
You have:
if ($youtube[$i] == 0){
}
else {
}
But POST vars are all strings. Change to:
if ($youtube[$i] == "0"){
}
else {
}
In otherwords, an equality on a string to numerical 0 will be true in your cases. And thus your else never executes.
*** Edit. PROOF
$test1 = "filename.dat";
$test2 = "2939";
$test3 = "some useful data";
$test4 = "0";
if ($test1 == 0) {
// Dont do anything
}
else {
echo "Do Work 1.";
}
if ($test2 == 0) {
// Dont do anything
}
else {
echo "Do Work 2.";
}
if ($test3 == 0) {
// Dont do anything
}
else {
echo "Do Work 3.";
}
if ($test4 == 0) {
// Dont do anything
}
else {
echo "Do Work 4.";
}
Only echoes out Do Work 2.. All other echo()s are not run because the equality of a string to a number 0 will return true except in those cases where the string also is numerical, and then the interpreter will compare the numerical values.
As how this relates to the OP's question: It can be inferred that the OP's POST vars must contain some non-numerical data because the OP insists that the 3rd array is populated:
But the third loop is not executed and there are real values on the variables inside the third array.
I will add likely the loop is executed, but not giving the expected the results due the reasons so mentioned.
Tested on PHP 5 and 7.
I am using a MVC Framework for my PHP projects and how it works is as follows:
We have the a root that's like http://localhost/myproject.
And it accepts an url like this: http://localhost/myproject/example-controller/example-function/params
So what it does is it goes to the example-controller and executes the function example-function where we have the possibility to add parameters like http://localhost/myproject/example-controller/example-function/1/2/3
So I have a dashboard where I want to be able to apply crud functionality to users and articles so the url becomes as follows:
http://localhost/myproject/dashboard/users, which means that it will execute the function users but let's say I want to create a new user the link then becomes dashboard/users/create but it will see create as a parameter and not as an actual function that needs to be executed.. so I understand I would have to make a UserController and a ArticleController but that means the link will become user/create or article/create in which case it doesn't use the DashboardController anymore.
If anyone understood what I tried to describe could someone come up with a possible explanation on how to solve this issue.
I have found a solution to my problem and rewrote the whole splitUrl() function so it now accepts what I need.
function splitUrl()
{
if (isset($_GET['url'])) {
require(ROOT. 'core/routes.php');
$tmp_url = trim($_GET['url'], "/");
$tmp_url = filter_var($tmp_url, FILTER_SANITIZE_URL);
$tmp_url = explode("/", $tmp_url);
$param = ['params' => [], 'found' => false];
foreach ($routes as $route => $value) {
//echo("We iterate over a new route called <strong>". $route. "</strong><br>");
$tmp_route = explode("/", $route);
// Variables for the length of the arrays;
$route_count = count($tmp_route);
$url_count = count($tmp_url);
//echo("The route count = <strong>". $route_count . "</strong> and the url count = <strong>". $url_count. "</strong><br>");
// Check if the length of the route in our acceptable routes array is equal to the length of the url.
if ($route_count == $url_count) {
$indexCount = null;
for ($i = 0; $i < $route_count; $i++) {
if ($tmp_route[$i] == '{param}') {
$indexCount = $i;
//echo("A parameter has been found at index <strong>" . $indexCount . "</strong><br>");
break;
}
}
if ($indexCount == null) {
$indexCount = count($tmp_route);
//echo("A parameter has NOT been found so the index has been set to <strong>". $indexCount. "</strong><br>");
}
$doContinue = false;
for ($i = 0; $i < $indexCount; $i++) {
//echo("We are comparing value <strong>". $tmp_route[$i] . "</strong> to <strong>". $tmp_url[$i] ."</strong><br>");
if ($tmp_route[$i] == $tmp_url[$i]) {
//echo("Value <strong>". $tmp_route[$i] ."</strong> appears to be valid<br>");
continue;
} else {
//echo("Value <strong>". $tmp_route[$i] ."</strong> appears to be invalid<br><hr>");
$doContinue = true;
break;
}
}
if ($doContinue) continue;
$count = 0;
for ($i = 0; $i < $route_count; $i++) {
if ($tmp_route[$i] == $tmp_url[$count]) {
$count++;
continue;
} else {
array_push($param['params'], $tmp_url[$count]);
$count++;
}
}
} else {
// This means the route that we are iterating over right now can not possibly be the one we are looking for as the length is not equal to the length of the url.
continue;
}
$param['controller'] = explode("#", $value)[0];
$param['action'] = explode("#", $value)[1];
return $param;
}
}
On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.
$random = rand(100,1000);
strpos(file_get_contents('website.com/url.php?username='.$username), $random);
So, until the code has managed to find the value of $random, it won't continue with the rest of the script. The updates can take about 10 seconds.
I have tried a while script:
$random = rand(100,1000);
echo '<div data-alert class="alert-box alert">'.$random.'</div>';
$key = false;
while($key){
if(strpos(file_get_contents('website.com/url.php?username='.$username), $random)) $key = true;
}
This doesn't seem to work.
On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.
If I understood your question correctly, try this:
$content = file_get_contents($url);
if(str_replace($string, "", $content) != $content)
echo "found";
EDIT:
do {
$content = file_get_contents($url);
} while(strpos($content, $string) === false);
EDIT 2:
$i = 0;
do {
$content = file_get_contents($url);
$i++;
} while(strpos($content, $string) === false && $i <= 100);
EDIT 3:
$i = 0;
do {
$content = file_get_contents($url);
$i++;
} while(strpos($content, $string) === false && $i <= 100);
if($i > 100){
echo "nothing found";
}
EDIT 4:
$startTime = time();
do {
$content = file_get_contents($url);
} while(strpos($content, $string) === false && time() - $startTime < 30);
if(time() - $startTime > 30){
echo "nothing found";
}
I am receiving the following error message "Header may not contain more than a single header, new line detected" I know it says that a new line has been detected, but I cannot figure where this line is coming from. I have tried to trim the variables..I have re-written the header line in different ways, without any result. I added the getallheaders function to see what was being passed, but I see no new line or any extra characters in the output $headers. Even using ob_start() does not help.
<?php
ob_start();
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (isset($_POST['st']))
$st = $_POST['st'];
else
$st = '0';
if (isset($_POST['num']))
$num = $_POST['num'];
else
$num = '0';
if (isset($_POST['type']))
$type = $_POST['type'];
else
$type = '0';
if (isset($_POST['rec']))
$rec = $_POST['rec'];
else
$rec = '0';
if (isset($_POST['option']))
$option = $_POST['option'];
else
$option = '0';
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
//$catalogObj->order_form_type_id = array();
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
//print "order_form_type_id: $order_form_type_id[$i]<br>";
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
//$url = "type=".$type."option=".$option."rec=".$rec."st=".$st."num=".$num;
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
//Header("location: search_catalog_handler.php?" . rawurlencode($url));
if (function_exists('getallheaders'))
{
$headers = getallheaders();
print_r( $headers);
}
exit;
}
}
function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
?>
First, thanks for the pointers! The problem in the above code was with the $st variable. I am not very experienced with headers and rewriting them but I had add the following conditinal statement:
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
To the beginning of my code, so it the complete code is:
<?php
ob_start();
/*************************************
altered complete 12/20/2013
rjm
*************************************/
include "catalog.obj";
session_start();
$catalogObj = $_SESSION['catalogObj'];
if (!empty($_POST['st']))
{
$st = $_POST['st'];
$num = $_POST['num'];
$type = $_POST['type'];
$rec = $_POST['rec'];
$option = $_POST['option'];
}
if(strcmp($_POST['submit'],"Reset Form") == 0)
{
header("location: search_catalog.php?type=$type&firstTime=1");
exit;
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
Header("Location: administration.php");
exit;
}
else
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
//exit;
$inventory_id_num = $_POST['inventory_id_num'];
$inventory_desc = $_POST['inventory_desc'];
$inventory_revision = $_POST['inventory_revision'];
$quantity = $_POST['quantity'];
$catalog_status_id = $_POST['catalog_status_id'];
$order_form_type_id = $_POST['order_form_type_id'];
$catalogObj->inventory_id_num = $inventory_id_num;
$catalogObj->inventory_desc = $inventory_desc;
$catalogObj->inventory_revision = $inventory_revision;
$catalogObj->quantity = $quantity;
$catalogObj->catalog_status_id = $catalog_status_id;
$catalogObj->order_form_type_id = $order_form_type_id;
$count=count($order_form_type_id);
for ($i=0; $i<$count; $i++)
{
if(strlen($order_form_type_id[$i]) > 0)
{
$catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
}
}
if(strcmp($_POST['submit'],"Back to Order Form") == 0)
{
Header("Location: order_form.php?num=$num");
exit;
}
else
{
Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");
exit;
}
}
?>
This allows for a specific type search (with parameters) and a general type search (no parameters) from the sending page.
Assuming that catalog.obj does not output any information to the browser (which would result in an error as well), your $type variable looks like the culprit since it's the only wildcard.
Note that you'll need to do the following for all POSTed variables in your script that you want to use in a URI:
Sine it's possible that $type could be anything (it's using the POSTed variable sometimes), you should clean it up before spitting it back out in your header:
$type = urlencode($type); // Prepares the variable to be inserted in the URI
header("Location: search_catalog.php?type=$type&firstTime=1");
I'm using this script to list a few Twitch.tv streams and their status (offline or online).
If there are no online streams found, I want it to display a text saying that all are offline.
Code that checks if the added streams are online:
//get's member names from stream url's and checks for online members
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
Full code:
<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php
$members = array("ncl_tv");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
function onlinecheck($online, $viewers) {
if ($online != null) {
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
$alloffline = "All female user streams are currently offline.";
function signin($person){
if($person != null){
return $person;
}
?>
</body>
</html>
............................................................................................................................................................................
Is it because your $userGrab URL contains usernames twice? This is the URL whose contents you're retrieving:
http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv
Having looked at the response, it doesn't look like it's causing the problem. The strange URL is a result of you appending to the $userGrab string in the first foreach loop, after you've already added them with the implode() function call before. I think twitch.tv is rightly ignoring duplicate channels.
If all the values in $checkedOnline are null, everyone is offline. Put this at the end of your first code sample:
$personOnline = false;
foreach($checkedOnline as $person) {
if($person !== null) {
$personOnline = true;
break;
}
}
if(!$personOnline) {
echo 'No one is online';
}
else {
//there is at least someone online
}