如何在Laravel上的app/Mail VeryfyMail.php上生成随机令牌?
使用Laravel 5.7,我有VeryMyMail系统。我需要在app/Mail VeryfyMail.php文件中发送一些随机数字到电子邮件主题 VeryfyMail.php如何在Laravel上的app/Mail VeryfyMail.php上生成随机令牌?,php,laravel-5,Php,Laravel 5,使用Laravel 5.7,我有VeryMyMail系统。我需要在app/Mail VeryfyMail.php文件中发送一些随机数字到电子邮件主题 VeryfyMail.php <?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class VerifyMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
//return $this->view('emails.verifyUser');
return $this->subject('')->view('emails.verifyUser');
}
}
既然你说你想让随机标记作为主题,那就做: 获取随机字符串(十六进制) 在创建令牌时,通常需要加密安全的东西,以使聪明的攻击者更难“猜测”令牌。幸运的是,PHP7中引入了PHP 这将创建一个十六进制随机令牌:
// Get some random bytes
$token = random_bytes(8);
// Since random_bytes() returns a string with all kinds of bytes,
// it can't be presented "as is".
// We need to convert it to a better format. Let's use hex
$token = bin2hex($token)
// Now just add the variable as the subject
return $this->subject($token)->view('emails.verifyUser');
获取随机数(整数)
如果您只想要数字,我们可以使用:
要生成随机十六进制字符串,可以使用,然后将其转换为十六进制。示例:
$random=bin2hex(随机字节(16))代码>。数字16
是你想要的随机字节数。你能告诉我如何在我的文件中使用这个吗?你的问题是什么?可以使用rand()
生成随机数。我可以在生成函数中使用它吗?@banda-你可以在任何你想要和需要的地方使用它。@Pie-你可以,但如果你关心安全性,就不应该。请注意,此函数不生成密码安全值,不应用于加密目的。如果需要加密安全值,请考虑使用RealthyIn()、RealthyByTeSe(或),或OpenSSLIVRAPIONPIDOYBYTESE()。-最好将上述代码包装成一个自定义函数,您可以在代码中使用。@Pie-生成令牌与加密无关。但是您确实希望使用最好的函数来创建随机字节(准确地说是伪随机字节),在PHP中,当前是random\u bytes()
。其他随机函数,如rand()
,在许多情况下可能就足够了,但对于令牌,您需要的是更难“猜测”的最佳随机字节。@Pie-因为令牌通常用作排序标识符。它可以用于电子邮件验证、访问API或其他任何用途。然后您肯定希望正确生成令牌,这样攻击者就不能“复制”令牌,因为这样他们就有可能访问某人的帐户或类似帐户。
// Generate the token. Add the min and max value
$token = random_int(1000000, 9999999);
// Use it as the subject
return $this->subject($token)->view('emails.verifyUser');