Search

segunda-feira, 10 de junho de 2013

PHP with SQLite3

SELECT

$file = 'db/emails.db';

$db = new SQLite3($file);

$result = $db->query('SELECT * FROM EMAILS;');

while ($row = $result->fetchArray()) {
      echo $row[0] . " - " . $row[1] . " - " . $row[2] ;
}

DELETE

$file = 'db/emails.db';
$db = new SQLite3($file);

$result = $db->exec('DELETE FROM EMAILS;');

INSERT

$db = new SQLite3($file);

$file = 'db/emails.db';
$country = $_SERVER["HTTP_CF_IPCOUNTRY"];
$email = trim($_POST['email']);

$query = $db->prepare('INSERT INTO EMAILS(EM_EMAILS, COUNTRY, ACTIVE) VALUES (:email,:country,:active)');

if($query){
$query->bindValue(':email',$email, SQLITE3_TEXT);
$query->bindValue(':country',$country, SQLITE3_TEXT);
$query->bindValue(':active',0, SQLITE3_INTEGER);
$result = $query->execute();
}else{
echo "error";
}

UPDATE

$file = 'db/emails.db';
$db = new SQLite3($file);

$result = $db->exec('UPDATE EMAILS SET EM_EMAIL="test" WHERE COUNTRY="PT";');

If you have any question or problem please leave a comment and i will try to help you ;)


domingo, 9 de junho de 2013

Why should i do something when there's millions already?!

Yep! That's your friends answer when you have an idea and ask them if they want to join you. After that will came the "how much i will win with that?", "i am not going to spend 10 dollars on that", "i need a project that doesn't exist yet, i don't need to invest (even $5) and will make me rich", that's the part i face-palm my self and ask "why god? why....".

So people can't invest $10/$15 dollars in a project, just because that wont make them rich, but they can spend $50 in cigarettes, drinks, and other shits? Ok, that's the kind of society i love(ironic). Lets don't make nothing, because i wont make any money with that!

What about experience an knowledge? Why everyone expect money from what they make?!

As example all of my projects until now were made thinking about letting people know me and my work. My
first project goal was to try to make something that reach 1000 users. The day i got that was fucking amazing! Today i have around 6000 users and i am making some money out of it, without doing nothing... but ofc, when i tryed it for the first time, some people said "why you going to make it?, there's things out there making the same?" AND!?!

Did you know that, there's around 2.405.518.376 internet users? maybe there's room for everyone ideas and projects! just don't give up because someone (that probably never done one decent project in their life) says that are millions already doing the same, go until the end and see what you get from it and you wont regret!

quinta-feira, 26 de abril de 2012

Java How To: Shuffle a List/Array

This is a simple way of how to shuffle an array or a list in Java, using Collections.shuffle(<List>);

There's 2 methods:


public static void shuffle(List list);



public static void shuffle(List list, Random rnd);

Usefull Link: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#shuffle(java.util.List)

You can use ArrayList(), or any other struct that extends <List>

You can found them here: http://docs.oracle.com/javase/6/docs/api/java/util/List.html

// Create the list
List list = new ArrayList();

// Shuffle the list - You will lose the older order
Collections.shuffle(list);

// ---------- SHUFFLE ARRAY ------------

// Create an array
String[] array = new String[20];

// Add elements

// Convert Array to List
List<String> aux = Arrays.asList(array);

// Shuffle the array
Collections.shuffle(aux);

// Convert the list to array again
array = aux.toArray();

Dedicated Web Hosting vs Shared Web Hosting

Hey!

Today i am going to talk a bit about Dedicated Web Hosting and Shared Web Hosting, specially the main differences and which is the best depending on the situation you are facing.

Let's start with shared hosting. In this type of service the objective its to give a decent host by having a low price. Normally any kind of normal website will found this as a solution to their host, because, its cheap compared to dedicated hosting, it's managed by the company that sells it, making it good for you since you don't need to waste time doing software or hardware maintenance but for the other-hand it have some problems as the name says, this service is shared - And what shared means here?

By saying that, it means. that all resources in the server are shared with every website hosted on that server.
CPU is shared, RAM is shared, IP is shared, bandwidth is shared making the sites sometimes run slowly, when someone makes a error on their website code or some site have a big traffic(normally big traffic websites are not allowed on shared hosting).

It brings some security problems and one of the biggest is the DDoS attack, if someone on that server get a DDoS your website will suffer with it too, making it unavailable for a certain period of time.

Dedicated web hosting is exactly the opposite of this, with it, you will have a % of the CPU, a % of the RAM, a % of the bandwidth and a IP reserved to your website. Dedicated service is normally used for high traffic websites and because resources are reserved to you it makes this service more expensive.

Anyway none of this (Dedicated or Shared web hosting) supports TCP/IP socket connection and rarely they will allow you to make connections to the database from a remote machine.

When you get to this point, the need of using sockets or remote db connection you will need to change your hosting type from shared/dedicated to a VPS or Dedicated server.

Shared Hosting PROS

  • Cheaper
  • Easier to manage
  • Allow "unlimited" resources for a low cost
  • Good for informative/static sites
Shared Hosting CONS
  • Very limited use
  • Do not allow socket connections
  • Do not allow remote database connections
  • Do not allow installation of plugins
  • Shared resources
Dedicated Hosting PROS
  • Dedicated resources
  • More secure
  • Good for bigger traffic websites
Dedicated Hosting CONS
  • More Expensive
  • Do not allow installation of plugins
  • Do not allow remote database connections
  • Do not allow socket connections


Conclusion


So as you can see if you are looking for a simply hosting service for a small website Shared web hosting is the answer, but if you need something more "sophisticated" you will need a dedicated hosting or maybe if too big a VPS or dedicated server (high traffic forums, Social networks, amazon like websites).

segunda-feira, 4 de julho de 2011

Swap Space

Today i am going to talk about Swap Space, not going to explain (for now) how to make the Swap Space but i will talk about what it is.
Many people don't know what is Swap Space, but in fact it is an important thing in Linux, so let's go ahead with it.

If you don't know, Linux uses pagination to change memory blocks(pages) between the main memory (RAM) and hard drive, this way the programs that are not running at the moment can place parts of their memory on the hard drive, giving the new process's more memory to run. So it's in this step that Swap Space comes in action, Swap is the name of the space on the disk given to the Linux kernel to do the pagination. Normally the Swap size it's 2x your Physical Memory size, if you have 1GB RAM your Swap will be of 2GB, since disk space it's cheap this is no problem, the problem is as you may know RAM access is made in nanoseconds while disk access is done in milliseconds, if you have a big Swap size it will slow down your programs, because the overhead of pagination will be bigger.

Exists two ways of making a Swap Space the Swap Partition and the Swap File. Swap Partition it's the best way and the more used since it creates a partition on the disk dedicated to the Swap, while Swap File creates a file of the size you want your Swap size to have and it will consume disk space.

Swap size plus the Physical size it's called the Virtual Memory, this way you can run programs that use more memory than you have physically, without receive the message "Error allocating memory: out of memory".

In the new kernels there's a new configuration related to the Swap it's called Swappiness.
Swappiness can have a value of 0 to 100 and the default is 60, this consists in the times that kernel do the pagination, higher the number, more times kernel will do pagination, smaller the number, less times kernel will change memory pages to disk.

Conclusion

Swap Space is important on Linux and you always should use it as a partition.
You can use a Linux without Swap, but if your machine runs out of memory your system will crash.
The rule to choose the size of the Swap size it's 2x your physical memory size.


This was my first article and i am sorry about my english.
Hope you enjoy, if you have any comments or doubt leave a comment

domingo, 3 de julho de 2011

How to: Disable IPTables

If you need to disable your iptables (not recommended) this is the way to do it.


Difficult: easy
Avg Time: 5min


You need to be logged as root to disable iptables.


Disable iptables on Ubuntu/Debian.

1º - Save your current iptables config, to restore later if needed.
iptables-save > /root/iptables.backup
2º -  Make this commands
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
3º - Now if you would like to restore your old config, do this
iptables-restore < /root/iptables.backup

Disable iptables Centos/RedHat/Fedora.


Disable iptables on Centos/RedHat/Fedora it's much simple!

1º - Save current config and Stop
/etc/init.d/iptables save
/etc/init.d/iptables stop
2º - Start iptables again 
/etc/init.d/iptables start

If you have any problem leave a comment

How to: Add a new user

Hello, in my first post i will explain how to add a new user to your system.

Difficult: easy
Avg Time: 1 min


Linux have a built in command called "useradd" that help people to add new user to the system.
To add a new user you must be logged as root or you will need to use sudo.
I will give some of the must useful examples.

Syntax: useradd options name

Example 1: Add new user to the system
useradd -r ricardo
Example 2: Add new user and do not create home directory (/home)
useradd -rM ricardo
Example 3: Add new user and create home directory in /home
useradd -rm ricardo
Example 4: Add new user and specify the home directory
useradd -rd /somedir/somedir ricardo
Example 5: Add a user and create a group with the same name
useradd -rn /somedir/somedir ricardo
Example 6: Add a user and add user to existent group
useradd -r -g groupname ricardo
Example 7: Add a user, create home directory and a group with same name
useradd -rmn ricardo

If you would like to set a password for the user i recommend you to use the passwd command instead of the -p option of useradd command.

Example 8: Create new user and set the password
useradd -rm ricardo
passwd ricardo

That's the way i use to add new user and set user password, if you have any other suggestion please post it as comment