How to use Action Cable with Devise

The websocket server is running in a separate process from the Rails application so to authenticate the user we need cookies.

  1. Set up cookies in Devise
    # app/config/initializers/warden_hooks.rb
    Warden::Manager.after_set_user do |user,auth,opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = user.id
      auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
    end
    
    # app/config/initializers/warden_hooks.rb
    ...
    
    Warden::Manager.before_logout do |user, auth, opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = nil
      auth.cookies.signed["#{scope}.expires_at"] = nil
    end
    ...
  2. Configure AC connection
    # app/channels/application_cable/connection.rb
    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
        def connect
          self.current_user = find_verified_user
          logger.add_tags 'ActionCable', current_user.name
        end
    
    protected
      def find_verified_user
        verified_user = User.find_by(id: cookies.signed['user.id'])
        if verified_user && cookies.signed['user.expires_at'] > Time.now
          verified_user
        else
          reject_unauthorized_connection
        end
      end
      end
    end
    

     

How to deal with mysqldump error 23: out of resources when opening file

So earlier today I was doing a mysql dump of a large database. And I got this error:

mysqldump: Got error: 23: "Out of resources when opening file './xxxx/xxxx' (Errcode: 24)" when using LOCK TABLE

A quick google reveals that it’s because the number of files that MySQL is permitted to open has been exceeded.

So I counted how many files our database has:

ls /var/lib/mysql/dbname/ -l|wc -l

The result is 8350 files.

Then checked the limit by executing this in phpmyadmin:

SHOW VARIABLES LIKE 'open%'

It gives me a result of 1024, so I opened /etc/my.cnf and added

[mysqld]
open_files_limit = 10000

Unfortunately this didn’t do the job!

Some further digging landed me on this stackexchange post: http://dba.stackexchange.com/questions/86987/mysql-open-files-limit-cannot-change-this-variable

Looks like the issue is systemd related.

Edit /usr/lib/systemd/system/mysqld.service  and add

LimitNOFILE=10000
LimitMEMLOCK=10000

Then run systemctl daemon-reload  and systemctl restart mysql.service .

Now with all that sorted, finally, the real deal:

mysqldump -u username -p dbname | gzip > ./dbexport.sql.gz

 

PHP can not connect RDS MySQL on an Amazon EC2 RHEL box

So I decided to use Amazon RDS for my blog. It’s fairly simple to set up RDS, but somehow I couldn’t get PHP to connect to RDS. WordPress kept throwing this error: “Error establishing a database connection”.

So I thought maybe my RDS security group settings are not correct? I opened the RDS instance to 0.0.0.0/0 and I was able to connect using mysql cli anywhere, including the RHEL box, but WP still gave me the same error. I then tried php mysql connection to RDS on another linux box and it worked!

OK, so that means somehow php mysql connection is not working on the RHEL box, what can be causing the problem? After a few Google searches, one post drew my attention, SELinux!! Of course!! How did I forget this thing…

So I went to /etc/selinux/config and set SELinux = disabled, after reboot, problem solved!

Of course you can add an exception rule but to avoid future headaches, better leave it disabled.

websitepanel更换服务的版本

Websitepanel虽然开源免费,但仍有很多不足。比起Plesk易用性差很多。

这次转移碰到个问题,之前是03的系统,转移后复制了原数据库,但服务的信息仍是旧的,面板又不支持自动更新服务的版本。这个时候需要根据Providers中的ServiceID来修改Services表中的数据,这样就可以了。

查询整个数据库中某个特定值所在的表和字段的方法

查询整个数据库中某个特定值所在的表和字段的方法

有时候我们想通过一个值知道这个值来自数据库的哪个表以及哪个字段,在网上搜了一下,找到一个比较好的方法,通过一个存储过程实现的。只需要传入一个想要查找的值,即可查询出这个值所在的表和字段名。

前提是要将这个存储过程放在所查询的数据库。


CREATE PROCEDURE [dbo].[SP_FindValueInDB]
(
@value VARCHAR(1024)
)        
AS
BEGIN
— SET NOCOUNT ON added to prevent extra result sets from
— interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @sql VARCHAR(1024) 
DECLARE @table VARCHAR(64) 
DECLARE @column VARCHAR(64) 

CREATE TABLE #t ( 
tablename VARCHAR(64), 
columnname VARCHAR(64) 

DECLARE TABLES CURSOR 
FOR 

SELECT o.name, c.name 
FROM syscolumns c 
INNER JOIN sysobjects o ON c.id = o.id 
WHERE o.type = ‘U’ AND c.xtype IN (167, 175, 231, 239) 
ORDER BY o.name, c.name 

OPEN TABLES 

FETCH NEXT FROM TABLES 
INTO @table, @column 

WHILE @@FETCH_STATUS = 0 
BEGIN 
SET @sql = ‘IF EXISTS(SELECT NULL FROM [‘ + @table + ‘] ‘ 
SET @sql = @sql + ‘WHERE RTRIM(LTRIM([‘ + @column + ‘])) LIKE ”%’ + @value + ‘%”) ‘ 
SET @sql = @sql + ‘INSERT INTO #t VALUES (”’ + @table + ”’, ”’ 
SET @sql = @sql + @column + ”’)’ 

EXEC(@sql) 

FETCH NEXT FROM TABLES 
INTO @table, @column 
END 

CLOSE TABLES 
DEALLOCATE TABLES 

SELECT * 
FROM #t 

DROP TABLE #t 

End

 

例如,要查询值’BBQ CHIC SW’,结果如下:

返回三条记录,说明这个值存在于三个表中,分别为_dts_menudef, g_dts_menudef和g_recipe中,字段名分别为name1, name1, name。

非常好用。

彻底解决由require(‘./wp-blog-header.php’)导致的500错误

这个问题困扰我好几天了,今天总算是搞定了。

之前查了很多资料,断断续续做了不少功课。但都没实质性进展。

刚开始的症状就是首页500错,但是后台可以正常进入。

查资料找到如下方案

/** Loads the WordPress Environment and Template */
require(‘./wp-blog-header.php’);

修改为

/** Loads the WordPress Environment and Template */
require(‘wp-blog-header.php’);

这还不算完,如果以后客户装WP,每次都这个错误,那会很麻烦。

于是继续找解决方案。

后来在某博文看到一篇文章,提到可能是因为上级目录没有相应权限,导致了require函数读取文件时遭到拒绝,于是给WEB根加上了IIS-WPG用户的读取权限,然后还原代码到

/** Loads the WordPress Environment and Template */
require(‘./wp-blog-header.php’);

至此,500错误消失,博客已经可以正常打开。

php 压缩网站上的文件,并返回下载

set_options ( array ('basedir' => dirname ($modpath ), 'inmemory' => 0, //不在内存压缩.而是直接存放到磁盘.如果要压缩下载,则可以选择为1
'recurse' => 1, //是否压缩子目录,resurse,递归的意思?
'storepaths' => 1, //是否存储目录结构,我选是。
'overwrite' => 1, //是否覆盖
'level' => 5, //压缩比
'name' => $zipFileName, //压缩最后生成的文件名,无需再次设置。这里是为了解说方便才放上来的。
'prepend' => "", //未知
'followlinks' => 0, //未知
'method' => 1, //未知
'sfx' => "", //自解压
'type' => "zip", //是zip还是tar...,无需设置,这里为了方便解说。放上来。
'comment' => "" ) );
$files = array("*.*");
//可以将文件名单独列出来加进去,但是文件名必须在basedir下,文件名支持*.*表示压缩全部。
$archive->add_files($files); //加$files数组里的文件名
// 正式写入磁盘
$archive->create_archive();
@header("Content-Disposition: attachment; filename=\"$zipFileName\"");
@header ( "Content-Length: " . filesize ($zipFileName ) );
@readfile( $zipFileName);
?>

纯代码实现首页调用分类图片

纯代码实现首页调用分类图片。给你的每一个wordpress分类目录指定一张图片,然后在首页文章中进行调用。原作者忘了是谁了,不好意思吖,如果你看到后联系我。功能如下:

1.CSS定义图片大小、位置;

2.图片格式可自由设置;

3.点击图片进入分类页面

我对此做了修改,去掉了链接功能,但保留title,有利于SEO,图片格式由gif变为jpg,更符合标准,修正了不规范的代码,可通过W3C检测。

在wp-content目录下新建文件夹cat-icons,将.jpg图片放进去。图片的名称必须与你的分类目录别名一致才行!比如你有一个分类是网络赚钱,它的别名是make-money,那么对应的图片应该命名为make-money.jpg。

将下面的代码拷贝到index.php文件中的

{
$catname =$cat->category_nicename;
$cattitle=$cat->cat_name;
echo “”<img src=’http://www.techmemo.net/wp-content/cat-icons/“;
\n”;
}
?>

再指定一个CSS样式:

.category {
float:left;
margin-right: 5px;
margin-bottom: 2px;
}
.category img {
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
}

Warning: Invalid argument supplied for foreach()

问题Warning: Invalid argument supplied for foreach() in 完善解决方案
将报错的语句做如下修改(例):

QUOTE:
foreach($extcredits as $id => $credit)
{
if($credit[‘ratio’])
{
$exchcredits[$id] = $credit;
}
}

改为

QUOTE:
if(is_array($extcredits)) //add
{
foreach($extcredits as $id => $credit)
{
if($credit[‘ratio’])
{
$exchcredits[$id] = $credit;
}
}
} //add

QUOTE:
foreach((array)$extcredits as $id => $credit)
{
if($credit[‘ratio’])
{
$exchcredits[$id] = $credit;
}
}