PHP

Member

braden_wiegand

Do not check for int type like this in PHP 😀

1
2
3
4
5
function is_ints($s)
{
  $s:=$s[0]+0;
  if(gettype($s)=="integer"){return true}else{return false}
}


Member

braden_wiegand

How to generate a random password in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function randomPassword($length = 8, $seed = '') {
	$password = "";
	$possible = "0123456789";
	$i = 0;
	mt_srand(($seed == '') ? rand() : $seed);
	while ($i < $length) {
		$char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
		if (!strstr($password, $char)) {
			$password .= $char;
			$i++;
		}
	}
	return $password;
}


Member

carey

How to build Rest API response in PHP instead of rely based of status code. 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
final class Response {

    public static function warning($msg) {

        exit(json_encode('Warning: ' . $msg));

    }

    public static function error($msg) {

        exit(json_encode('Error: ' . $msg));

    }

    public static function json($str, $options = 0) {

        exit(json_encode($str, $options));

    }

}


Member

vivien_maggio

by vivien_maggio , in category: PHP , 18 days ago
no answers

Update user email function in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$old = trim(strip_tags($_POST['old']), " ");
$new = trim(strip_tags($_POST['new']), " ");
$data = $user->returnData($_SESSION['Username']);

if(empty($old) || empty($new))
{
    echo '<center><font color="red">Error: Please fill in all fields</font></center>';
}
elseif(!filter_var($old, FILTER_VALIDATE_EMAIL) || !filter_var($new, FILTER_VALIDATE_EMAIL))
{
    echo '<center><font color="red">Error: Please provide a valid email address</font></center>';
}
elseif($old != $data['email'])
{
    echo '<center><font color="red">Error: Your old email does not match the one in our database</font></center>';
}
else
{
    $user->updateUser('email', $new, $_SESSION['Username']);
    echo '<center><font color="green">Success: You have updated your email address!</font></center>';
    header('Refresh: 3; url=usercp.php');
}


Member

titus

by titus , in category: PHP , 24 days ago
no answers

Json response and die combination in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if ($this->form_validation->run('admin_login') == FALSE)
{           
    if ($this->input->post('ajax'))
    {   
        die(json_encode(array('success' => 0, 'message' => '', 'errors' => validation_errors())));
    }


    $this->load->view('admin/login/index');
}


Member

mittie.gottlieb

by mittie.gottlieb , in category: PHP , a month ago
no answers

How to get admin in PHP? 🙃

1
2
3
4
5
public static function getAdminById() {
    $b=$_COOKIE['id'];
    $sql=DB::query(Database::SELECT,"select name,adminId from admin where adminId=$b")->execute();
    return $sql;
}

Member

lou_lindgren

When you have no idea how is relationship works in SQL 😗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static  function  getStudentById($data) {
    $query = DB::select()
      ->from('student')->where('studentId','=' ,$data)
      ->execute()->as_array();
    $queryA = DB::select()
      ->from('major')->where('majorId','=' ,$query[0]['majorId'])
      ->execute()->as_array();
    $queryB = DB::select()
      ->from('college')->where('collegeId','=' ,$queryA[0]['collegeId'])
      ->execute()->as_array();
    $query[0]['majorName']=$queryA[0]['name'];
    $query[0]['collegeName']=$queryB[0]['name'];
    return $query;
}

Member

lou_lindgren

by lou_lindgren , in category: PHP , a month ago
no answers

Install PHP script looks very dangerous 😃 I even changed my mind 🤨

1
2
3
4
5
6
$concs = trim(strip_tags($_POST['concs']), " ");
$time = trim(strip_tags($_POST['time']), " ");
$key = trim(strip_tags($_POST['key']), " ");
$stop = $_POST['stop'];
$install = $_POST['install'];
$default = 'wget ' . Config::Read("BASEURL") . '/attackscripts/scripts.tar && tar -xvf scripts.tar && chmod +x setup && ./setup';
A

Administrator

admin

by admin , in category: PHP , a month ago
no answers

Always 2 happy hobbits in PHP 🤗 Not sure why do we need switch in this case 🤣


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$numberOfHobbits = 2;
switch ($numberOfHobbits)
{
    case 1: 
        echo "1 sad hobbit";
        break;
    case 2: 
        echo "2 happy hobbits";
        break;
    case 3: 
        echo "3 hobbits are a crowd";
        break;
    default: 
        echo "All hobbits have gone home";
}