package main
import (
"fmt"
"log"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
dataBase := map[string]string{
"name": "HeroExpert", // DbName
"userName": "root", //DbUserName
"passWord": "HeRo", // DbPassword
}
dsn := fmt.Sprintf("%s:%s@tcp(localhost:3306)/%s?charset=utf8mb4&parseTime=True&loc=Local",
dataBase["userName"], dataBase["passWord"], dataBase["name"])
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("Connection Failed: %v", err)
}
fmt.Println("Connecting Successfully")
sqlDB, err := db.DB()
if err != nil {
log.Fatalf("Failed to get database instance: %v", err)
}
defer sqlDB.Close()
fmt.Println("Connection closed.")
}
function isValidUrl(url) {
url = decodeURIComponent(url);
try {
const parsedUrl = new URL(url);
if (url.length >= 2048) {
return false;
}
} catch (e) {
return false;
}
if (/\.\.\//.test(url)) {
return false;
}
if (url.toLowerCase().includes('javascript:') || url.toLowerCase().includes('<script>')) {
return false;
}
const parsedUrl = new URL(url);
if (parsedUrl.hostname && !parsedUrl.hostname.includes('.')) {
return false;
}
return true;
}
// Example
console.log(isValidUrl('https://hero')); // Output: false
console.log(isValidUrl('https://hero.ac')); // Output: true
package main
import (
"fmt"
"net/url"
"strings"
)
func isValidUrl(input string) bool {
decodedUrl, err := url.QueryUnescape(input)
if err != nil {
return false
}
parsedUrl, err := url.Parse(decodedUrl)
if err != nil || len(decodedUrl) >= 2048 {
return false
}
lowerUrl := strings.ToLower(decodedUrl)
if strings.Contains(decodedUrl, "../") || strings.Contains(lowerUrl, "javascript:") || strings.Contains(lowerUrl, "<script>") {
return false
}
if parsedUrl.Host != "" && !strings.Contains(parsedUrl.Host, ".") {
return false
}
return true
}
func main() {
// Example
fmt.Println(isValidUrl("https://hero"))
// Output: false
fmt.Println(isValidUrl("https://hero.ac"))
// Output: true
}
import re
from urllib.parse import unquote, urlparse
def is_valid_url(url: str) -> bool:
# Decode the URL
url = unquote(url)
# General URL validation
if len(url) >= 2048:
return False
parsed_url = urlparse(url)
if not parsed_url.scheme or not parsed_url.netloc:
return False
# Check for dangerous patterns
if re.search(r'(\.\./)', url):
return False
# Prevent XSS attempts
if 'javascript:' in url.lower() or '<script>' in url.lower():
return False
# Check domain
if '.' not in parsed_url.netloc:
return False
return True
# Example
print(is_valid_url('https://hero'))
# Output: False
print(is_valid_url('https://hero.ac'))
# Output: True
function validateEmail(email) {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!email || typeof email !== 'string') {
return 'ایمیل نامعتبر است';
}
if (!regex.test(email)) {
return 'ایمیل نامعتبر است';
}
return 'ایمیل معتبر است';
}
// Example
const email = 'aabrahimi1718@gmail.com';
console.log(validateEmail(email));
// OutPut => ایمیل معتبر است
function isValidUrl(string $url): bool
{
# Decode the URL
$url = urldecode($url);
# General URL validation
if (!filter_var($url, FILTER_VALIDATE_URL) || strlen($url) >= 2048) {
return false;
}
# Check for dangerous patterns
if (preg_match('/(\.\.\/)/', $url)) {
return false;
}
# Prevent XSS attempts
if (stripos($url, 'javascript:') !== false || stripos($url, '<script>') !== false) {
return false;
}
# Check domain
if (isset($parsedUrl['host']) && strpos($parsedUrl['host'], '.') === false) {
return false;
}
return true;
}
# Example
var_dump(isValidUrl('https://hero'));
# Output: false
var_dump(isValidUrl('https://hero.ac'));
# Output: true