how to use elseif statement in dom php html find - php

How to add statement, when I search and it doesnt exist on the url, it will show nothing.html?
$url1 = "http://www.pengadaan.net/tend_src_cont2.php?src_nm=";
$url2 = $_GET['src_nm']."&src_prop=";
$url3 = $_GET['src_prop'];
$url = $url1.$url2.$url3;
$html = file_get_html($url);
if (method_exists($html,"find")) {
echo "<ul>";
foreach($html->find('div[class=pengadaan-item] h1[] a[]') as $element ) {
echo ("<li>".$element."</li>");
}
echo "</ul>";
echo $url;
}
else {
}

There are two ways to move to another page in PHP. you can do header("Location: http://www.yourwebsite.com/nothing.php"); or you can have PHP echo JavaScript to do a reidrect (if you already defined your headers):
if (method_exists($html,"find")) { // If 'find exist'
...
} else { // Otherwise it does not exist
header("Location: http://www.pengadaan.net/nothing.php"); // redirect here
}
Or if you already sent you headers you can get around it using JavaScript:
...
} else {
echo '<script>window.location.replace("http://www.pengadaan.net/nothing.php")</script>';
}

Related

Cannot Redirect Using $_SERVER Variables, PHP

I have a PHP application (a request form) that first checks for an active $_SESSION before a user can access the site. Because of the timeout period set for this form there is rarely an active session. Here's the check:
if (isset($_SESSION['samlUserdata'])) {
$attributes = $_SESSION['samlUserdata'];
$user_department = $attributes['department'];
$user_email = $attributes['email'];
$user_employee_id = $attributes['employee_id'];
$user_full_name = $attributes['full_name'];
}
...and here is the else {} that I use to grab the REQUEST_URI:
else {
if (isset($_SERVER['REQUEST_URI'])) {
$referer = $_SERVER['REQUEST_URI'];
$redirect = "https://myinternalwebsite.net$referer";
}
header("Location: https://myinternalwebiste.net/confirm_auth.php?sso");
}
...and last, here is what I do with the $_GET
if (isset($_GET['sso'])) {
if (isset($redirect)) {
$auth->login($redirect);
} else {
$auth->login("https://myinternalwebsite.net/");
}
}
However, once my session is killed I am never properly routed back to the URL set in the ['REQUEST_URI'], I am always just dumped onto the internal site's front page. I have troubleshooted this on and off for some time over the last week, to no avail. I've tried other variables in the $_SERVER array as well, such as ['REDIRECT_URL'].
I'm at a loss, and I'm sure this fairly simple for anyone with more experience than myself... so I am all ears and eager to learn.
EDIT:
Thank you for the comments below. Per your advice I will add the entirety of my code here, removing only the unnecessary parts. (And yes, I appreciate the tip to flip the initial (isset()) to (!isset(). Thank you for that.)
<?php
session_start();
$auth = new OneLogin\Saml2\Auth($saml_settings);
if (isset($_SESSION['samlUserdata'])) {
$attributes = $_SESSION['samlUserdata'];
$user_department = $attributes['department'];
$user_email = $attributes['email'];
$user_employee_id = $attributes['employee_id'];
$user_full_name = $attributes['full_name'];
} else {
if (isset($_SERVER['REQUEST_URI'])) {
$referer = $_SERVER['REQUEST_URI'];
$redirect = "https://example.net$referer";
}
header("Location: https://example.net/confirm_auth.php?sso");
}
if (isset($_GET['sso'])) {
if (isset($redirect)) {
$auth->login($redirect);
} else {
$auth->login("https://example.net/");
}
} else if (isset($_GET['slo'])) {
$auth->logout();
} else if (isset($_GET['acs'])) {
$auth->processResponse();
$errors = $auth->getErrors();
if (!empty($errors)) {
echo '<p>', implode(', ', $errors), '</p>';
}
if (!$auth->isAuthenticated()) {
echo "<p>Not authenticated!</p>";
exit();
}
$_SESSION['samlUserdata'] = $auth->getAttributes();
if (isset($_POST['RelayState']) &&
OneLogin\Saml2\Utils::getSelfURL() != $_POST['RelayState']) {
$auth->redirectTo($_POST['RelayState']);
}
} else if (isset($_GET['sls'])) {
$auth->processSLO();
$errors = $auth->getErrors();
if (empty($errors)) {
echo '<p>Sucessfully logged out!</p>';
} else {
echo '<p>', implode(', ', $errors), '</p>';
}
}
?>

Use PHP $_GET[ for displaying different content in the same PHP file - is there another way to do it?

Normally I use $_GET to catch the URL parameters and display content dependend on what the parameter is.
<?php
function isCmd($cmd) {
if(isset($_GET["cmd"]))
if($_GET["cmd"] == $cmd)
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the normal page</p>";
}
The url would look like these
example.com/script.php
example.com/script.php?cmd=page1
example.com/script.php?cmd=page2
example.com/script.php?cmd=page3
And it works as expected, but I see on websites they can do just:
example.com/script.php?page1
example.com/script.php?page2
example.com/script.php?page3
and it would work, without cmd=
How can I do the same in PHP?
I answered it myself after looking into the $_GET supervariable. It was easier than I thought.
function isCmd($cmd) {
if(isset($_GET["$cmd"]))
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the standard page</p>";
}
Also works with $_GET.
It would work with $_SERVER["QUERY_STRING"] too.
You can use $_SERVER["QUERY_STRING"] which will return the string after the "?" in the URL.

check external links invalid in a web site

i wrote a script on php in order to check external links invalid in a web site
this is the sript
<?php
// It may take a whils to spider a website ...
set_time_limit(10000);
// Inculde the phpcrawl-mainclass
include_once('../PHPCrawl_083/PHPCrawl_083/libs/PHPCrawler.class.php');
include ('check.php');
// Extend the class and override the handleDocumentInfo()-method
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo(PHPCrawlerDocumentInfo $DocInfo) {
if (PHP_SAPI == "cli") $lb = "\n";
else {
$lb = "<br />";
// Print the URL and the HTTP-status-Code
// Print the refering URL
$file = file_get_contents($DocInfo->url);
preg_match_all('/<a[^>]+href="([^"]+)/i', $file, $urls);
echo '<br/>';
$home_url = parse_url( $_SERVER['HTTP_HOST'] );
foreach($urls as $url){
for($i=0;$i<sizeof($url);$i++){
$link_url = parse_url( $url[$i] );
if( $link_url['host'] != $home_url['host'] ) {
if (check_url($url[$i])=== false){
echo " Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;
echo '<br/>';
echo "<font color=green >"."lien externe invalide :".$url[$i].$lb." </font>";
echo '<br/>';
}
}
}
}
}
}
}
$crawler = new MyCrawler();
$crawler->setURL("http://www.tunisie-web.org ");
$crawler->addURLFilterRule("#\.(jpg|gif|png|pdf|jpeg|css|js)$# i");
$crawler->setWorkingDirectory("C:/Users/mayss/Documents/travailcrawl/");
$crawler->go();
?>
but more than external links(not invalid :/) it gives "http://www.tunisie-web.org" as an axternal link and i don't know where is the problem !!
please help
and this is check.php :
<?php
function check_url($url) {
if ( !filter_var($url, FILTER_VALIDATE_URL,FILTER_FLAG_QUERY_REQUIRED) === false) {
return true ;
}
else {
return false;
}
}
?>

PHP include pages error

Still trying to get how PHP works :)
Kindly help me with one solution & idea, here is when i'm using:
<?php
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if(in_array($_GET['show'], array_keys($content))) {
include($content[$_GET['show']]);
} else {
include('sub_id00.php');
}
?>
and:
<?php
$content = array(
'id00'=>'N/A',
'id01'=>'ID01',
'id02'=>'ID02',
);
if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
echo $content[$_GET['show']];
} else {
echo $content['id00'];
}
?>
Where first example includes pages and second includes simple code inside ''
Problem is that if there is no ID set (index.php) ut shows default page/code. And if wrong ID set, it'll also show default page/code.
How to update it so without any ID set it would show default page and if some wrong ID set, it would show some error page/code?
Cheers!
UPDATE!
After a while reqding i've updated it with:
<?php
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if (in_array($_GET['show'], array_keys($content)))
{
include($content[$_GET['show']]);
}
elseif (isset($_GET['show']))
{
include('sub_error.php');
}
else {
include('sub_id00.php');
}
?>
And:
<?php
$content = array(
'error'=>'error msg',
'id00'=>'N/A',
'id01'=>'ID01',
'id02'=>'ID02',
);
if(!empty($_GET['show']) && isset($content[$_GET['show']]))
{
echo $content[$_GET['show']];
}
elseif (isset($_GET['show']))
{
echo $content['error'];
}
else
{
echo $content['id00'];
}
?>
:)
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if (isset($_GET['show']))
{
if (array_key_exists($_GET['show'], $content))
{
//$_GET id is set and it exists in content
include($content[$_GET['show']]);
}
else
{
//$_GET id is set but does not exist in content
//include whatever page you have for a wrong id here
}
}
else
{
//else no $_GET was set
//include default page
}

Generating XML file through PHP results in a minor issue . Please help

In order to generate a XML i am using following Code currently.
.<?php
require ('../dbconfig/dbconfig.php');
$appId = $_GET['id'];
$sqlTabs = "Select _id,tab_title,position from tab_info where app_id=$appId order by position ";
$resultTabs=mysql_query($sqlTabs );
$countTabs=mysql_num_rows($resultTabs);
if($countTabs>0)
{
echo '<application applicationName="sample app">';
echo '<tabs>';
while ($rowTab = mysql_fetch_array($resultTabs) ) {
echo '<tab id="t'.$rowTab['_id'].'" tabtitle="'.$rowTab['tab_title'].'" position="'.$rowTab['position'].'" data="somedata">';
$sqlTabItems = "Select _id as tabitemId,item_type,item_title,item_data from items_info where tab_id=".$rowTab['_id']." and parent_id=0 order by _id ";
$resultTabItems=mysql_query($sqlTabItems);
$countTabItems=mysql_num_rows($resultTabItems);
if($countTabItems>0)
{
$level = 1;
echo '<listItems>';
while ($rowTabItem = mysql_fetch_array($resultTabItems) ) {
echo '<listItem id="'.$rowTabItem['tabitemId'].'" parentId="t'.$rowTab['_id'].'" itemtype="'.$rowTabItem['item_type'].'" itemtitle="'.$rowTabItem['item_title'].'" itemdata="'.$rowTabItem['item_data'].'" level="'.$level.'">';
giveitems($rowTabItem['tabitemId'],$rowTab['_id'],$level);
echo '</listItem>';
}
echo '</listItems>';
}
else
echo 'No Data';
echo '</tab>';
}
echo '</tabs></application>';
}
else
{
echo 'No Data';// no tabs found
}
function giveitems($pitemId,$tabId,$level)
{
$sqlItems = "Select _id,item_type,item_title,item_data from items_info where parent_id=".$pitemId." order by _id ";
$resultItems=mysql_query($sqlItems);
$countItems=mysql_num_rows($resultItems);
$numbers = 0;
if($countItems>0)
{
$level = $level +1;
echo '<listItems>';
while ($rowItem = mysql_fetch_array($resultItems) ) {
echo '<listItem id="'.$rowItem[0].'" parentId="'.$pitemId.'" itemtype="'.$rowItem[1].'" itemtitle="'.$rowItem[2].'" itemdata="'.$rowItem[3].'" level="'.$level.'">';
/*********** Recursive Logic ************/
if(giveitems($rowItem[0],$tabId,$level)==1)
{
echo '</listItem></listItems>';
return 1;
}
else
{
echo '</listItem></listItems>';
return -1;
}
}
}
else
{
echo 'No Data';// no tabs found
return -1;
}
}
?>
Now in this code i want to replace all echo statements with a String variable and then i want to write that String variable content into a file , the porblem is if i define the string variable at the top of the file even then also that string variable cannot accessible from the giveItems functions below.
Pls suggest some solution
To access a global variable from within a function you need to write global $varname; at the top of that function's definition. Example:
function giveitems($pitemId,$tabId,$level) {
global $xmlstring;
// ....
}
However, this is pretty bad practice nowadays and you'd be better off writing a class that will both produce the XML and write it to a file.

Categories