Get the full URL in PHP

Have a look at $_SERVER[‘REQUEST_URI’], i.e. $actual_link = “http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”; (Note that the double quoted string syntax is perfectly correct) If you want to support both HTTP and HTTPS, you can use $actual_link = (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] === ‘on’ ? “https” : “http”) . “://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”; Editor’s note: using this code has security implications. The client can … Read more

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource

A query may fail for various reasons in which case both the mysql_* and the mysqli extension will return false from their respective query functions/methods. You need to test for that error condition and handle it accordingly. mysql_ extension: NOTE The mysql_ functions are deprecated and have been removed in php version 7. Check $result … Read more

How do I get the current date and time in PHP?

The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions are called to. I’m in Melbourne, Australia so I have something like this: date_default_timezone_set(‘Australia/Melbourne’); Or another example is LA – US: date_default_timezone_set(‘America/Los_Angeles’); You can also see what … Read more

Where can I find php.ini?

On the command line execute: php –ini You will get something like: Configuration File (php.ini) Path: /etc/php5/cli Loaded Configuration File: /etc/php5/cli/php.ini Scan for additional .ini files in: /etc/php5/cli/conf.d Additional .ini files parsed: /etc/php5/cli/conf.d/curl.ini, /etc/php5/cli/conf.d/pdo.ini, /etc/php5/cli/conf.d/pdo_sqlite.ini, /etc/php5/cli/conf.d/sqlite.ini, /etc/php5/cli/conf.d/sqlite3.ini, /etc/php5/cli/conf.d/xdebug.ini, /etc/php5/cli/conf.d/xsl.ini That’s from my local dev-machine. However, the second line is the interesting one. If there is … Read more

Returning JSON from a PHP Script

While you’re usually fine without it, you can and should set the Content-Type header: <?php $data = /** whatever you’re serializing **/; header(‘Content-Type: application/json; charset=utf-8′); echo json_encode($data); If I’m not using a particular framework, I usually allow some request params to modify the output behavior. It can be useful, generally for quick troubleshooting, to not … Read more

PHP array delete by value (not key)

Using array_search() and unset, try the following: if (($key = array_search($del_val, $messages)) !== false) { unset($messages[$key]); } array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a false-y value on success … Read more

What is the difference between public, private, and protected?

You use: public scope to make that property/method available from anywhere, other classes and instances of the object. private scope when you want your property/method to be visible in its own class only. protected scope when you want to make your property/method visible in all classes that extend current class including the parent class. If … Read more

What is stdClass in PHP?

stdClass is just a generic ’empty’ class that’s used when casting other types to objects. Despite what the other two answers say, stdClass is not the base class for objects in PHP. This can be demonstrated fairly easily: class Foo{} $foo = new Foo(); echo ($foo instanceof stdClass)?’Y’:’N’; // outputs ‘N’ I don’t believe there’s … Read more

How do I expire a PHP session after 30 minutes?

You should implement a session timeout of your own. Both options mentioned by others (session.gc_maxlifetime and session.cookie_lifetime) are not reliable. I’ll explain the reasons for that. First: session.gc_maxlifetime session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during session start. But the garbage collector … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)