Admin auto login for a cron job
Hi, I need to put some script from admin folder in cron job on my server. In that script I use some function from admin/includes/application_top.php . So I use require_once ('includes/application_top.php'); for call that function. When I call this script he need my interaction to login first like admin. Now, I need information on witch way I can create auto login on admin from my script.
Best regards
Re: Admin auto login for a cron job
Here's one approach, which has basic security around it. You can enhance it further if you wish, but this should work as-is:
1. In your cron PHP script that calls application_top, put this *before* the call to application_top:
Code:
define('CRON_ADMIN_USER_ID', 99);
You can substitute 99 with whatever admin user id *number* you want, as long as it exists.
So, when you create the user, it'll be assigned a number. THAT is the number you need to enter in the define() statement mentioned above.
This number will be used to authenticate login, and for tracking admin activity in the admin activity log.
2. In your /admin/init_includes/init_admin_auth.php script, add the new lines as shown below:
Code:
if (! defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}
if (!isset($_SESSION['admin_id']) && defined('CRON_ADMIN_USER_ID') && (int)CRON_ADMIN_USER_ID != 0) {
$_SESSION['admin_id'] = CRON_ADMIN_USER_ID;
}
if (!(basename($PHP_SELF) == FILENAME_LOGIN . '.php')) {
Re: Admin auto login for a cron job
My host uses a GUI for cron job's, so i'm not able to add any lines.
Would there be a differend way to achive the same?
Re: Admin auto login for a cron job
Quote:
Originally Posted by
Robbie_79
My host uses a GUI for cron job's, so i'm not able to add any lines.
Would there be a differend way to achive the same?
you dont need to add a line before your cronjob
you have to create a file in the admin folder
for example cron1.php
PHP Code:
<?php
// filename: cron1.php
// make changes in /admin/init_includes/init_admin_auth.php
define('CRON_ADMIN_USER_ID', '13');
define('EMAIL_USE_HTML', false);
require ('includes/application_top.php');
// your code to run via cron
// sends an email listing all orders with status $os
$os = 1;
$sql = 'SELECT orders_id, date_purchased, order_total, payment_method, customers_name FROM ' . TABLE_ORDERS . ' WHERE orders_status=' . $os;
$res = $db->execute($sql);
$content = 'orders_status: ' . $os . "\n\r\n\r";
while (!$res->EOF){
$content .= implode(', ', $res->fields) . "\n\r";
$res->MoveNext();
}
echo 'CRON1 ' . date('Ymd H:i:s');
zen_mail('Admin', STORE_OWNER_EMAIL_ADDRESS, 'ZC open orders', $content, 'ZC Cron1', STORE_OWNER_EMAIL_ADDRESS );
your cronjob command could look like:
Code:
/usr/bin/lynx -accept_all_cookies -dump http://localhost/PathTo/admin/cron1.php
Re: Admin auto login for a cron job
thank you very much! it's a first-cron for me ;)
Re: Admin auto login for a cron job
Will this method also make the cron page browser-accesible?
Re: Admin auto login for a cron job
Quote:
Originally Posted by
Robbie_79
Will this method also make the cron page browser-accesible?
if you add a line like
PHP Code:
echo "<hr>" . $content;
at the end of the file cron1.php you can display the content (which is send via mail) at the console; the syntax is the same as the cron-job-syntax
but simple: try it
if you need a linux environment under windows :: http://jars.de/english/ubuntu-804-vm...wnload-english to test the console commands
Re: Admin auto login for a cron job
I can't get it to work, im not experienced enough so help would be highly appriciated.
Let me write the full story,
What I want to achive is an automated status update.
I created a new admin page where I can manually batch-update a couple of filtered orders (I.E. all orders older then 7 days with status X and payment method Y etc).
The way I do this is by calling
PHP Code:
http://mydomain/admin/cron_status_update.php?action=order_remind
I though doing this by using WGET, its not the prettyest method but it should work
The cron command line looks like:
PHP Code:
/usr/local/bin/wget -O /dev/null -q http://mydomain/admin/cron_status_update.php?action=order_cancel
My apache log shows:
PHP Code:
serverip - - [21/Mar/2009:17:59:00 +0100] "GET /admin/cron_status_update.php?action=order_cancel HTTP/1.0" 302 578 "-" "Wget/1.10.2"
serverip - - [21/Mar/2009:17:59:00 +0100] "GET /admin/login.php?zenAdminID=8e55e4a77660107242ae112b52be12e HTTP/1.0" 200 1709 "-" "Wget/1.10.2"
worth mentioning is that I made the edits in init_admin_auth.php and defined the cron_admin_user_id in my cron_status_update.php file
Probably i'm trying something silly. I hope someone can point me out whats wrong
Re: Admin auto login for a cron job
Quote:
Originally Posted by
Robbie_79
the cron1-example do a similar thing, so please try the cron1 example. if this dont work, then please look again at /admin/init_includes/init_admin_auth.php
you can try it directly in the browser: you must be logged-out; if the login displays, something went wrong
Re: Admin auto login for a cron job
Quote:
Originally Posted by
hugo13
you can try it directly in the browser: you must be logged-out; if the login displays, something went wrong
then thats the problem... I cant acces the file directly when i'm logged out, it redirects me to login.php.
I rechecked the edits DrByte gave 10 times for typo's, i can asure you that aint the problem.
I have the Admin Profiles addon installed, maybe thats whats makes the difference?