Foreigners are all those who do not have Spanish nationality. However, not all of them are subject to the same legislation when they are in Spain. We can distinguish:
The general regime will deal with the rights and freedoms of persons who are nationals of third countries, thus considering those who are not Spanish or nationals of countries belonging to the European Union or the European Economic Area, as well as their family members (subject to the Community regime).
We can find some exclusions, such as those affecting people (and their families) who belong to international or intergovernmental organizations or bodies with headquarters in Spain.
Regulations: Organic Law 4/2000, of 11 January, on the rights and freedoms of foreigners in Spain and their social integration and Royal Decree 557/2011, of 20 April.
The Community regime applies to the rights of entry into and stay in Spain of nationals of Member States of the European Union, of the other States party to the Agreement on the European Economic Area (Norway, Iceland and Liechtenstein) and of nationals of the Swiss Confederation.
It also applies to family members (regardless of nationality) of Spanish nationals and nationals of the above-mentioned countries provided that they reside in Spain, being the latter:
The spouse or analogous partner, whether registered or not.
The descendants and those of the spouse.
The ascendants and those of the spouse.
Certain members of the family who meet certain requirements, known as the extended family.
Regulations: Royal Decree 240/2007, of 16 February, on the entry, free movement and residence in Spain of citizens of European Union Member States and other States that are parties to the European Economic Area Agreement
It regulates the regime that Spain must apply both to non-EU foreign nationals and to stateless persons who flee their country of origin or residence because they have a well-founded fear of persecution, or danger that could endanger their life. This is the so-called international protection, which includes both asylum and subsidiary protection.
Regulations: Law 12/2009, of 30 October, regulating the right to asylum and subsidiary protection