ملفات Cookies مقابل الجلسات
اختيار المكان المناسب لتخزين الحالة
ملفات Cookies مقابل الجلسات درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Ways to Remember
Both cookies and sessions help your app remember a visitor, but they store data in very different places. Knowing which to pick matters. 🤔
What a Cookie Is
A cookie is a small piece of text the browser keeps and sends back on every request to your site. The data lives on the client.
Setting a Cookie
You set a cookie on the response object with set_cookie, giving it a name, a value, and optionally a lifetime.
response = HttpResponse('Hi')
response.set_cookie('lang', 'en', max_age=3600)Reading a Cookie
Cookies arrive in request.COOKIES, a simple dictionary. Read your value back by its key on the next request.
lang = request.COOKIES.get('lang', 'en')Where Session Data Lives
A session keeps the real data on the server and sends the browser only a key. The visitor never sees the contents.
Cookies Are Visible
Because cookies sit in the browser, the user can read and edit them. So never store secrets like prices or user roles there.
Sessions Hide the Data
Session data stays server-side, so it is the right home for anything sensitive or anything the user must not tamper with.
Size Limits
Cookies are tiny, capped near 4KB each, while sessions can hold much larger structures since they live in your database or cache.
Protect Your Cookies
Add HttpOnly so JavaScript cannot read a cookie, and Secure so it is only sent over HTTPS. Django sets these for sessionid by default.
response.set_cookie('t', '1', httponly=True, secure=True)When to Use a Cookie
Reach for a plain cookie for small, non-secret preferences like a theme or a chosen language that is fine for the user to see.
When to Use a Session
Use a session for login state, shopping carts, or anything private. It safely backs the cookie with trusted server storage.
Quick Check
You need to store a value the user must never read or change. What fits best?
Recap
You compared cookies and sessions: cookies store small, visible data on the client, sessions keep larger, private data on the server. Pick by sensitivity and size. 🎉
الأسئلة الشائعة
هل درس «ملفات Cookies مقابل الجلسات» مجاني؟
نعم — نص درس «ملفات Cookies مقابل الجلسات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «ملفات Cookies مقابل الجلسات»؟
اختيار المكان المناسب لتخزين الحالة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «ملفات Cookies مقابل الجلسات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إطار الجلسات
- قراءة request.session وكتابته
- إطار الرسائل
- ملفات Cookies مقابل الجلسات