I would like to protect a directory and authenticate users against a mysql database. I am using lighttpd and haven't been able to find a way of doing so. Is it possible?
You could use mod_auth, here is the relevant doc page
Since it has no direct access to a database, i would recommend using the 'htdigest' method, and regenerating the file from your database users.
the 'htdigest' format is just: "user:realm:md5(password)", as explained in the page.
Generating a file like this from a php script should be extremely simple.
pseudo-code:
foreach ($users as $user) {
// $user['md5pass'] = md5($user['password']);
$line = sprintf("%s:%s:%s\n", $user['username'], 'protected', $user['md5pass']);
file_put_contents('htdigest-file', $line, FILE_APPEND);
}
Also, from the same page, here is a sample lighttpd configuration for mod_auth:
auth.backend = "htdigest"
auth.backend.htdigest.userfile = "lighttpd-htdigest.user"
auth.require = ( "/download/" =>
(
# method must be either basic or digest
"method" => "digest",
"realm" => "download archiv",
"require" => "user=agent007|user=agent008"
),
"/server-info" =>
(
# limit access to server information
"method" => "digest",
"realm" => "download archiv",
"require" => "valid-user"
)
)
Related
There is currently 25k+ users we have in database. All users are subscribed to a common topic All.
I have two directories inside public_html. First is for API built in codeigniter. This API is used to provide data for all adnroid and iOS devices.
Second directory is for admin panel built in Laravel. We use it to upload data and also to send notification to firebase topics.
Both API and Admin Panel share same database.
If we send notification to topics which is not subscribed by much
users, there is no issue. But If I send notificaiton to a topic
which has much users It causes problems on our backend. The API
stops responding, or sometimes takes too long to respond.
Sometimes also admin panel stops responding too.
I am so confused because all the things are handled by firebase. I just make one API call.
Can anyone explain what's causing the problem?
Or any possible reason?
Update
use Kreait\Firebase\Messaging;
use Illuminate/Support/Http/Request;
trait UserTrait {
public function notify(Request $request, Messaging $messaging) {
$message_hi = array(
"notification_type" => $notification_type,
"notification_title" => $notification_title_hi,
"icon_image" => $icon_image,
"notification_description" => $request->notificationText_hi,
"image_url" => $request->image_url,
);
$message = array(
"notification_type" => $notification_type,
"notification_title" => $notification_title,
"icon_image" => $icon_image,
"notification_description" => $request->notificationText,
"image_url" => $request->image_url,
);
$commodityIdArray = $request->cId
//to send all
if($request->notification_type == 1) {
$messaging->sendAll([
['topic' => 'All', 'data' => $message],
['topic' => 'All_hi', 'data' => $message_hi],
]);
} else {
//to send to a fourite topic subscribed by some users
//Prepare Condition for both hindi and english users
$topic_condition = "";
$topic_condition_hi = "";
foreach($commodityIdArray as $topic) {
$topic_condition .="'".$topic."' in topics && ";
$topic_condition_hi.="'".$topic."_hi' in topics &&";
}
//send notification to hindi and english topics
$messaging->sendAll([
['condition' => substr($topic_condition, 0, -3), 'data' =>
$message],
['condition' => substr($topic_condition_hi, 0, -3), 'data' =>
$message_hi],
]);
}
}
Use Queue
You have to use a Queue which set your process in queue and when one process completes, the second one starts
you can also set number of retries of your process
I have several WordPress sites hosted in my server, and some of them have the W3 Total Cache plugin installed. Right now I flush the cache for each site manually, but I want to do it programatically and all together.
I'm trying to write a PHP script that will do this. I was looking at the code used by the "Clean Cache All" option for this plugin in the dashboard, and is this piece here:
if ( $modules->plugin_is_enabled() ) {
$menu_items['10010.generic'] = array(
'id' => 'w3tc_flush_all',
'parent' => 'w3tc',
'title' => __( 'Purge All Caches', 'w3-total-cache' ),
'href' => wp_nonce_url( network_admin_url(
'admin.php?page=w3tc_dashboard&w3tc_flush_all' ),
'w3tc' ));
So I'm guessing there are 2 ways to do what I need:
1- Make a web request, but that would probably require authentication.
2- Instance the class and use the method w3tc_flush_all();
This is what I have thus far:
function clean_cache($web)
{
global $path;
$path2 = $path.$web["directory"]."/httpdocs/wp-content/plugins/w3-total-cache/";
if(file_exists($path2)) //Tienen TotalCache instalado
{
require_once $path.'w3-total-cache-api.php';
//if(class_exists("w3-total-cache-api.php"))
//{
$plugin_totalcache = & w3_instance("w3-total-cache-api.php");
//if(function_exists('w3tc_dbcache_flush'))
$plugin_totalcache->w3tc_fush_all();
//}
}
}
Any help will be much appreciated.
I am trying to clean up my site by putting all of my configurations in one place for easy access.
I have many different configuration dependencies for example, PayPal and Stripe public/private and sandbox/live keys as well as a number of links e.g. google recaptcha links.
I don't want to be spreading these keys around my app and then need to go hunting for them if I want to go from sandbox to live for example.
I am trying to define my API keys and most used links in the CodeIgniter config.php file like this...
$config['stripe_live'] = [
'secret' => 'secret_key_xyz',
'private' => 'private_key_xyz',
]
$config['stripe_sandbox'] = [
'secret' => 'secret_key_xyz',
'private' => 'private_key_xyz',
]
$config['paypal'] = [
'secret' => 'secret_key_xyz',
'private' => 'private_key_xyz',
]
$config['recaptcha'] = [
'site_key' => 'xyz_one_two_three',
'secret_key' => 'xyz_one_two_three',
];
$config['jquery'] = [
['jquery_link'] => base_url() . 'Public/js/jquery.js',
]
$config['bootstrap'] = [
['bootstrap_link'] => base_url() . 'Public/js/jquery.js',
]
$config['fontawesome'] = [
]
$config['google_fonts'] = [
];
$config['groupworld'] = [
'groupworld_api' => 'api_key_xyz';
];
Question one:
If I wanted to access my Stripe live private key I would have to write...
$stripe_live = $this->config->item('stripe_live');
$stripe_live['public_key'];
This is almost as much work as just copying the key to where I need it (one or two places). So is there a simpler way?
Question two:
Is is okay to put my urls in the config file like in my example above? Or would it be better to define my URLs as constants (in the constants file) and then simply access them as constants instead of writing out $this->config->item('bootstrap_link')
Thanks.
After looking at the CodeIgniter Config documentation I have come up with the following solution at least for my API configuration settings, in the example below I am using the google recaptcha API.
1 - Make a new file inside of the application/config folder and call it whatever you want... e.g. api_config.php
Inside this file put your API keys like this:
// stripe api
$config["stripe_live_public_key"] = "public_key_xyz";
$config["stripe_live_private_key"] = "public_key_xyz";
$config["stripe_sandbox_public_key"] = "public_key_xyz";
$config["stripe_sandbox_private_key"] = "public_key_xyz";
// paypal api
$config["paypal_live_public_key"] = "public_key_xyz";
$config["paypal_live_private_key"] = "public_key_xyz";
$config["paypal_sandbox_public_key"] = "public_key_xyz";
$config["paypal_sandbox_private_key"] = "public_key_xyz";
// recaptcha api
$config["recaptcha_api_url"] = 'https://www.google.com/recaptcha/api.js';
$config["recaptcha_verification_url"] = "https://www.google.com/recaptcha/api/siteverify";
$config["recaptcha_public_key"] = "lfgksl;dfg;kkkkdsjfhskjfhkjsdhfjshjksjdh";
$config["recaptcha_private_key"] = "sfkljslfjsjfahjjjjjjhjhsdfjskhajkakkajdj";
// groupworld api
// phpmailer api
2 - In the controller file load your config file and mass the data to the view like this...
$this->config->load('api_config');
$data['recaptcha_api_url'] = $this->config->item('recaptcha_api_url');
$data['recaptcha_public_key'] = $this->config->item('recaptcha_public_key');
3 - In the view file simply display your data...
<script src="<?php echo $recaptcha_api_url; ?>"></script>
<div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_public_key; ?>"></div>
Now to change your config data in multiple places simply go to the api_config.php file and paste in your new keys.
As I'm a newbie can't comment :/ .
I will start with question 2. Its ok to keep like this. But stripe,paypal are payment gateways it will be good to store it in db as Yogesh said and retrieve to use it.It will also comes in handy if you want to provide user to edit it.
For js,css links you can put them in a view like 'includefiles.php' and load it in all pages as we load views.
for easy retrieval of your data, you can use helper functions.
<?php
//paymentdetail_helper
function getpaymentdetailhelper(someid or gateway name as arg eg.$id){
$ins=& get_instance();
$ins->load->database();
//your queries $ins->db->query();
return $data;
}
?>
Save this in application/helpers as paymentdetail_helper.php and load it as usual. more info about helpers in questionInfo about helper
Its my idea. :) You're welcome with suggestions
Google Analytics, by just placing its sourcecode on my website, automatically tracks everything I used to need (pageviews, unique visitors).
But now, I need to track events, and the only way to do this is to do it server-side. Each time any users does an specific action i need to track, the server posts data to google to track the information, as explained here:
https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event
And it does works amazingly perfect, but, since I realiced, I am now receiving a LOT of visits from Spain, doubling the visits from USA. And before I implemented the event tracking, Spain wasn't even part of the top 10 countries.
Today I have realiced that my servers are in Spain, and that may be causing the issue.
How can I track the event, without making it count as a pageview?
$url = 'http://www.google-analytics.com/collect';
$data = array('v' => '1', 'tid' => 'UA-HIDDEN-1', 'cid' => $_SERVER["REMOTE_ADDR"], 'ni' => '1', 't' => 'event', 'ec' => '', 'ea' => 'JUMP', 'el' => '');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
Thank you very much!!
You are sending the IP adress as a client id, which is wrong. For one, the client id is supposed to be an UUID. Secondly, Analytics won't recognize that these events belong to an existing user.
You'd need to grab the existing client id for an existing user on the web page:
ga(function(tracker) {
var clientId = tracker.get('clientId');
});
and then send it back to the server and use it in your request (1). At the moment GA cannot assign correct geo information since the events do not belong to the session of the user who initiates the event (this quite possibly affects some other metrics, too).
(1) You might as well read the GA cookie in PHP, but Google recommends against it since the cookie format might change without notice. The script above will always return a correct client id even if the cookie format changes.
Updated: I have read a bit more documentation and while my answer seems still somewhat relevant it's probably wrong for the actual use case - Geo is determined by IP and the serverside script will still send the servers IP. So quite possibly (haven't done the science yet) this would look like one visitor with two devices instead of a single visitor.
Update 2: Apparently it is now possible to include the users IP adress as parameter, so this answer is possibly no longer relevant.
Here is a techopad presentation about mixing UA client- and serverside, maybe that helps.
An event in and of itself is not a pageview. See: Event Tracking
Is there a specific reason why you need to track events server side and pageviews from the normal ga.js client-side code?
You can easily track events from the client side, if you were unaware of that:
Click Link to Track Event
Assuming that you needed to keep events AND pageviews on the server side:
<?php
//Put SERVER_ADDR into a var
$request_ip = $_SERVER['REMOTE_ADDR'];
// Put any server IPs you need to filter out below in an array
$localhosts = array('127.0.0.1','192.168.15.1','10.1.10.1');
// Use this later
$url = 'http://www.google-analytics.com/collect';
Now, Figure out what to do with the REMOTE_ADDR check if its in our list above. then build an array of type to send GA (events, pageviews)
$actions = array();
// Note that the values are arbitrary and will let you do what you need.
if(in_array($request_ip)){
//Only track event, or track pageview differently, or track two events.
$handle_myServer = true;
$actions = ('event');
} else {
// Track everyone else
$handle_myServer = false;
$actions = ('event','pageview','mySpecialPageview','mySpecialEvent');
}
Finally We have built a list of events we can use in flow control with existing code for pageviews, user timing, events, etc. Be creative!
foreach($actions as $action){
$data = null; $options=null;
if($handle_myServer){
$someFlagForGA = 'RequestFromSpainServer';
}
if($action == 'event'){
$data = array('v' => '1'
, 'tid' => 'UA-HIDDEN-1',
,'cid' => $request_ip
,'ni' => '1'
, 't' => 'event'
, 'ec' => $someFlagForGA,
,'ea' => 'JUMP', 'el' => ''
);
} elseif($action == 'pageview'){
$data = array('v' => '1', 'tid' => 'UA-HIDDEN-1'
, 't' => 'pageview'
, 'dh'=> 'yourGAenabledDomainHere.com'
, 'dp'=> 'ViewedPage.html'
, 'dt'=> 'homepage'.' SERVER VISITED '.$someFlagForGA
);
} else {
// Do whatever else
}
// Would be better to do below with a single function
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
) ,$data);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context) or die('Error!!');
}
?>
I would like to make a web service stub where I can test my client. I have written down
some JSON like this:
{
"response": {
"success": true,
"meta" : "tags",
"data": [
{
"first_name" : "John",
"last_name" : "Doe",
"age" : 30
},
{
"first_name" : "Jane",
"last_name" : "Doe",
"age" : 25
}
]
}
}
I have no PHP experience to talk about.
How can I make a
webserver.com/get_names.php?first_name=john&last_name=doe&age=30
script that returns the above JSON.
It should not do any evaluation on the parameters, that is just how it will end up being called when implemented against a server, I would just like it to return the JSON string correctly so my JSON parser can run through it as if it was the actual server response.
Hope someone can help me out, thank you in advance.
Save the JSON string as-is and call the file get_names.php. Don't forget to pick UTF-8 in your editor's save as dialogue. Nothing will be parsed as PHP if there isn't a single <?php string in the file.
You may also want to put this on top of the file:
<?php
header('Content-Type: application/json');
?>
This is usually referred to as an API, and can be developed on many levels.
First level would be standard streamline php, where you would have code that follows the general php coding.
$Action = isset($_GET["action"]) ? $_GET["action"] : false;
switch($Action)
{
case 'get_names':
//fetch data and display.
break;
}
The next method and the simplest would be to use an MVC Application Framework, I would recommend Code Igniter for this as tis bigginner friendly and the URI Structure is similar to an API Soap Server.
After copying your CI Files to your /api/ path, you would go threw the guide and configure your database,libraries,helpers etc, you would also use mod_rewrite to set up the URI Convention to get urls such as /api/get/games/API_KEY
your class would look like so:
class Get extends Controller
{
public function __construct()
{
parent::Controller();
}
public function games($Api = false,$limit = 10, $offset = 0)
{
if(!$Api)
{
show_error("API Key require to fetch games");
}
if(your_api_check($api) === true)
{
//Load the games
$games = $this->models->games->get($limit,$offset);
$this->output->send(json_encode($games));
}
}
}
Obviously more extensive checking with the validation of the params but you will get the gist of it.
The next level would be very complex for your self but if you wish to persue the idea then you may want to look into Simple Object Access Protocol but ill leave that for you to decide.
Links:
Codeigniter
RESTful with Codeigniter
Restful with CodeIgniter #2
Codeigniter XMLRPC Services
You could save this as get_names.php on your web server.
<?php
$my_associative_array = array(
'response' => array(
'success' => true,
'meta' => 'tags',
'data' => array(
array(
'first_name' => 'John',
'last-name' => 'Doe',
'age' => 30,
),
array(
'first_name' => 'Jane',
'last_name' => 'Doe',
'age' => 25
),
),
),
);
echo json_encode($my_associative_array);
Alternatively, you could just create a .txt file that looks precisely like your JSON.
You could also just point it to my server, where the above script is operational: http://dorkitude.com/example_4725873.php