Linear codes constructed from defining sets have been extensively studied and may have a few nonzero weights if the defining sets are well chosen. Let $${\mathbb {F}}_q$$Fq be a finite field with $$q=p^m$$q=pm elements, where p is a prime and m is a positive integer. Motivated by Ding and Ding's recent work (IEEE Trans Inf Theory 61(11):5835---5842, 2015), we construct p-ary linear codes $${\mathcal {C}}_D$$CD by $$\begin{aligned} {\mathcal {C}}_D=\{{\mathbf {c}}(a,b)=\big (\text {Tr}_m(ax+by)\big )_{(x,y)\in D}: a, b \in {\mathbb {F}}_q\}, \end{aligned}$$CD={c(a,b)=(Trm(ax+by))(x,y)źD:a,bźFq},where $$D \subset {\mathbb {F}}_q^2$$DźFq2 and $$\text {Tr}_m$$Trm is the trace function from $${\mathbb {F}}_q$$Fq onto $${\mathbb {F}}_p$$Fp. In this paper, we will employ exponential sums to investigate the weight enumerators of the linear codes $${\mathcal {C}}_D$$CD, where $$D=\{(x, y) \in {\mathbb {F}}_q^2 \setminus \{(0,0)\}: \text {Tr}_m(x^{N_1}+y^{N_2})=0\}$$D={(x,y)źFq2\{(0,0)}:Trm(xN1+yN2)=0} for two positive integers $$N_1$$N1 and $$N_2$$N2. Several classes of two-weight and three-weight linear codes and their explicit weight enumerators are presented if $$N_1, N_2 \in \{1, 2, p^{\frac{m}{2}}+1\}$$N1,N2ź{1,2,pm2+1}. By deleting some coordinates, more punctured two-weight and three-weight linear codes $${\mathcal {C}}_{\overline{D}}$$CD¯ which include some optimal codes are derived from $${\mathcal {C}}_D$$CD.