file_get_contents() working on localhost but not working on online server - file-get-contents

$result = file_get_contents(ADMIN_URL.'/content/'.$name.'.php');
if($result){
echo "YES";exit;
}else{
echo "NO";exit;
}
Output is NO,
where ADMIN_URL is "turkjammat.com/demo/administrator/"
and $name is mardumshumari
which makes url in file_get_contents() funciton "turkjammat.com/demo/administrator/content/mardumshumari.php"
and the file is there in above url, this code is working fine in my local xamp server but when i deploying it on my hosting server its not working. plese help some body. Thanks

Your hosting server typically does NOT have the same folder hierarchy for user home directories as your local server.
Run php_info() on your hosting server to find out what the correct path is.
Look for DOCUMENT_ROOT in the Environment section.
A simple phpinfo.php file looks like this:
<?php
phpinfo();
?>

Try this:
<?php
$url = 'your_url.php';
$opts = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create($opts);
$result = file_get_contents( $url , NULL, $context );
echo $result;
?>

Related

Using Powershell to create a web application in a folder that is not an application

I want to create a web application in IIS that does not live at the root of the IIS site.
i.e. MySite/beta/WebApplication.
This is my starting point:
New-WebApplication "WebApplication" -Site "MySite" -ApplicationPool "MyAppPool" -PhysicalPath "C:\Sites\MySite\beta\WebApplication"
That creates me the physical structure I want C:\Sites\MySite\beta\WebApplication , but makes IIS look like this:
MySite (IIS Web Site)
WebApplication (IIS WebApplication)
beta (Folder)
WebApplication (Folder)
Is there a way this can be done via powershell? I do not really want beta to be a web application, just a folder.
I know this post is a little older but here is a powershell script I wrote that converts an existing folder to a web application in IIS or if it doesn't exist creates a new folder and web app. It also creates the app pool for it as well. It receives an array of app names so you can create more than one web application. This was my first powershell script so if you have any suggestions feel free to comment.
#Receives an array of appnames and creates the app pools and web applications or converts the folder to an application
Param([parameter(Mandatory=$true)][string[]]$appNames)
$useDefaultPhysicalPath = Read-Host "Would you like to use the default physical path? (C:\inetpub\wwwroot\)";
Import-Module WebAdministration;
$physicalPath = "C:\inetpub\wwwroot\";
if(!($useDefaultPhysicalPath.ToString().ToLower() -eq "yes" -or $useDefaultPhysicalPath.ToString().ToLower() -eq "y"))
{
$physicalPath = Read-Host "Please enter the physical path you would like to use with a trailing \ (do not include the app name)";
}
$appPath = "IIS:\Sites\Default Web Site\";
foreach($appName in $appNames)
{
if((Test-Path IIS:\AppPools\$appName) -eq 0)
{
New-WebAppPool -Name $appName -Force;
}
if((Test-Path $appPath$appName) -eq 0 -and (Get-WebApplication -Name $appName) -eq $null)
{
New-Item -ItemType directory -Path $physicalPath$appName;
New-WebApplication -Name $appName -ApplicationPool $appName -Site "Default Web Site" -PhysicalPath $physicalPath$appName;
}
elseif((Get-WebApplication -Name $appName) -eq $null -and (Test-Path $appPath$appName) -eq $true)
{
ConvertTo-WebApplication -ApplicationPool $appName $appPath$appName;
}
else
{
echo "$appName already exists";
}
}
Since you are using the same physical file repository that is used by the "MySite" collection, it will create the "beta" folder. If you place this new web application in its own path (i.e., "C:\Sites\WebApps\WebApplication") you will get the desired results. The code below worked for me.
New-WebApplication "TestingViaPosh" -Site "Default Web Site" -ApplicationPool "DefaultAppPool" -
PhysicalPath "C:\Users\MyUserId\Documents\TestWebApp"
EDIT: To create a web application in a folder underneath the root of a website, you need to first create the folder in the site you desire (i.e., "C:\Sites\MySite\Beta"). Then the Powershell command will look like this:
New-WebApplication "TestingViaPosh" -Site "Default Web Site\Beta" -ApplicationPool "DefaultAppPool" -PhysicalPath "C:\Users\MyUserId\Documents\TestWebApp"
Since some people use the term folder and Virtual Directory, I thought it would be worth posting how you create a New Web Application inside a Folder / Virtual Directory. Which looks like this in IIS Manager.
You simply need to use the Path as the site name.
Import-Module WebAdministration
$SiteName = "Act.Web.API" # IIS Site Name
New-WebVirtualDirectory -Site $SiteName -Name APFW-api -PhysicalPath C:\inetpub\wwwroot
New-WebApplication -Name Act.Web.API -Site $SiteName\APFW-api -PhysicalPath "C:\Program Files (x86)\ACT\APFW-api" -ApplicationPool $SiteAppPool
$SiteName\APFW-api equals "Act.Web.API\APFW-api" at run time so the highlighted application is created.

Split-Path with Root UNC Directory

So I've got a UNC path like so:
\\server\folder
I want to get just the path to server, eg \\server.
Split-Path "\\server\folder" -Parent returns "". Anything I try which deals with the root, fails.
For example, Get-Item "\\server" fails too.
How can I safely get the path of \\server from \\server\\folder in PowerShell?
By using the System.Uri class and querying its host property:
$uri = new-object System.Uri("\\server\folder")
$uri.host # add "\\" in front to get exactly what you asked
Note: For a UNC path, the root directory is the servername plus the share name part.
An example using regular expressions:
'\\server\share' -replace '(?<!\\)\\\w+'
$fullpath = "\\server\folder"
$parentpath = "\\" + [string]::join("\",$fullpath.Split("\")[2])
$parentpath
\\server

Get dbname from multiple web.config files with powershell

I would like to issue a powershell command to return me the connection string (specifically I am looking for the db name value) for all the web sites on a web server...
So I would like to see something like
site1 dbname=Northwind
site2 dbname=Fitch
site3 dbname=DemoDB
I have tried using the IIS Powershell snap-in... I thought I was close with this:
PS C:\Windows\system32> Get-WebApplication | Get-WebConfiguration -filter /connectionStrings/*
but... after looking at the results... my answer doesn't appear to be in there
I am very new to powershell - so excuse my ignornance and inexperience
Any help appreciated!
thanks!
Hopefully, this will get you started. This just assumes there will be a web.config file at the physical path of the web application's physical path. It does not recurse to find other web.config files in the web application. It also assumes your connection strings are in the connectionStrings configuration element.
Import-Module WebAdministration
Get-WebApplication | `
ForEach-Object {
$webConfigFile = [xml](Get-Content "$($_.PhysicalPath)\Web.config")
Write-Host "Web Application: $($_.path)"
foreach($connString in $webConfigFile.configuration.connectionStrings.add)
{
Write-Host "Connection String $($connString.name): $($connString.connectionString)"
$dbRegex = "((Initial\sCatalog)|((Database)))\s*=(?<ic>[a-z\s0-9]+?);"
$found = $connString.connectionString -match $dbRegex
if ($found)
{
Write-Host "Database: $($Matches["ic"])"
}
}
Write-Host " "
}
This post may give you an idea to start with. Basically load in the web.config file as an XML file and then just find the node where the connection string is.
Do something like $myFile = ([xml] Get-Content web.config). You can then pipe that to Get-Member ( $myFile | Get-Member -MemberType Property) to start working your way into the file to see what node has it. I'm not at a computer where I can show you some screenshots to explain it more, but you can check this chapter out from PowerShell.com "Master PowerShell" e-book that explains working with XML very well.

How to run a php script in cron

I have found many questions and articles about this but i still have some difficulties.
I'm using the following command
/usr/bin/php home/domain.com/public_html/cron/script.php
I receive the following error
Status: 404 Not Found
X-Powered-By: PHP/5.2.8
Content-type: text/html
No input file specified.
i'm using Cpanel, the file is hosted on domain.com/cron/script.php
Anyideas, thanks :p
Put a leading slash on the script name, i.e.
/usr/bin/php /home/domain.com/public_html/cron/script.php
Unless you actually intend to run the script through the web, as in lacqui's answer, and you don't mind random third parties being able to run it any time they like, there's no reason you should put it inside your public_html directory; quite the opposite.
Try:
wget -O - http://domain.com/cron/script.php
and see if you get a better result.
Edit: added "- O - " to not write output to home folder.
You might need to use the binary known as php-cli instead of just php.
I'm realising that it is an old question and that you may have found a solution but none of the answers above helped me and I was getting the same 404 error when I was running a cron script.
The problem was related to the way in which the path to the php script was written. The path must start from public_html like this /usr/bin/php public_html/public/index.php
In several shared hosting wget and curl commands are not available from cron. If one wants to execute a web (http) request from cron, then it can be done by calling the desired web url as php curl inside cron php script.
Below is an example code to be put inside cron php file:
<?php
function callRemoteHttp($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$ret_arr = array('data' => $result, 'status_code' => curl_getinfo($curl, CURLINFO_HTTP_CODE));
curl_close($curl);
return $ret_arr;
}
$ret = callRemoteHttp('http://example.com?param1=value1&param2=value2');
?>

Change IIS Site Home Directory w/ Powershell

I can query the AD and find all the IIS sites and their virtual directories, now I need to be able to update those home directories and save the changes.
After I fetch the directory entry I can display the site path using $site.Path, however setting it doesn't seem to have any effect. It never changes the actual stored path.
I have tried $site.Path = <new path> and $site.Put( "Path", <new path> ) but neither have these seem to be affecting the stored path.
$site = $iis.psbase.children |
where {$_.keyType -eq "iiswebserver"} |
where {$_.psbase.properties.servercomment -eq $siteConfig.name };
$s = [ADSI]($site.psbase.path + "/ROOT");
$s.Path
# $s.Path = $siteConfig.path
# $s.Put("Path", $siteConfig.path )
$s.psbase.CommitChanges()
Import-Module WebAdministration
Set-ItemProperty 'IIS:\Sites\Default Web Site\' -name physicalPath -value $siteConfig.path
http://technet.microsoft.com/en-us/library/ee909471(WS.10).aspx
Ok, I tried this and it seems to work:
$s.psbase.properties.path[0] = $siteConfig.path
$s.psbase.CommitChanges()
Is there a better cleaner way of handling this?

Resources