Web Development Questions That Don't Need Their Own Thread v2
3,079 replies, posted
[QUOTE=Richy19;30619190]What language is youtube made in?[/QUOTE]
I always thought it was python, along with google and the google KB. I could be wrong though.
python
[QUOTE=vepa;30618948]You want to store that in the session and not as a plain cookie.
In your code, the $fusername etc. variables are going out of scope from the while loop - so don't use one, just get the values from the first row that fetch_assoc returns (since the id is unique anyway).
Also, don't echo from the function - return the value instead.[/QUOTE]
Thanks. I'm not sure if I understand you when you say to get the values from the first row. How would it look as code? Also, how would I store the id in a session? Would I have to check to see if a cookie exists to start a new session each time? I hear that sessions can be hijacked or something, so is there a way to make them secure?
Sorry, noob here.
[QUOTE=alphaspida;30619944]Thanks. I'm not sure if I understand you when you say to get the values from the first row. How would it look as code? Also, how would I store the id in a session? Would I have to check to see if a cookie exists to start a new session each time? I hear that sessions can be hijacked or something, so is there a way to make them secure?
Sorry, noob here.[/QUOTE]
1. Just remove the while loop completely. Use mysql_fetch_assoc to set $row as you already are doing and set the $fwhatever variables from $row.
2. [url=http://www.tizag.com/phpT/phpsessions.php]Check this out for sessions[/url].
[release][h2]Cookies[/h2]
A cookie is a bit of text data sent by the site to be stored on the client's computer.
[quote]HTTP/1.1 200 OK
Server: nginx/1.0.2
Date: Wed, 22 Jun 2011 01:19:05 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.2.13
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
[b]Set-Cookie: PHPSESSID=baf16896bfe25f69fbb71571c33e489d; path=/[/b]
[/quote]
this cookie, which is now stored on the client's computer, is sent back to the server whenever you make a request to it.
[quote]GET /tiger/ HTTP/1.1
Host: example.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
[b]Cookie: PHPSESSID=baf16896bfe25f69fbb71571c33e489d[/b][/quote]
You can store multiple cookies on a client computer without any trouble. Because of the properties of cookies, they are commonly used to identify users for websites that have dynamic webpages, or for analytics (see google analytics).
In php, you can access the cookies with the $_COOKIE array. Each cookie is included in the array, as the name as the key and the value as, well, the value. You can set cookies in php with the setcookie(); function, and the simplest way to remove a cookie is setting it's expiration date in the past like this: [php]<?php setcookie("PHPSESSID", "", time()-3600); ?> [/php]
Cookies are also modifiable using javascript. the document.cookie object has a list of the cookies that the site currently has. You can add cookies by doing something like this: [code]document.cookie = name+'='+value+'; expires=Fri, 1 Jan 2012 24:00:00 UTC; path=/';[/code] You can get a list of cookies from the document.cookie object, and you can delete a cookie the same way you added it (I think).
Cookies are website-dependent. A cookie you have on one site will not transfer to another. cookies can also be path-dependent, but default to the root of a domain address, which normally includes all files on the server that you can access. They are also set to expire at a certain time, whether that maybe when the browser closes or the end of next year.
Here are some cookies that you might find on any site:
[img]http://dl.dropbox.com/u/11275736/sessid.png[/img]
Cookies will last until they expire or are deleted, and will continue to be sent back to the server for as long as they live.
[/release]
You might ask, "But Ac!dL3ak, how does that tie back into sessions?" The answer is quite simple: sessions use cookies to identify the clients.
[release]
[h2]Cookies, Sessions, PHP, and YOU![/h2]
Before you can even begin to use sessions, you need to start your session. To do that, all you have to do is stick this simple line of code on on every page you want to use the sessions on: [php]<?php session_start(); ?>[/php]
This lets php know that it will require the session cookie. If no session cookie (most commonly named as PHPSESSID) is sent from the client, then php sends the client the session cookie and starts a new one. Each client has his or her own sessions, so the session variable changes from each client to the next.
[img]http://dl.dropbox.com/u/11275736/sesscook.png[/img]
Each session (from what I'm heard) is stored in a particular file in a temporary folder on the server.
[img]http://dl.dropbox.com/u/11275736/s/sessions.PNG[/img]
Sessions are typically used for logins, as it's imperative that information like that is not modifiable by the end user.
If a user visits any php page that has session_start(); at the top and a modification to the $_SESSION variable, that modification will be carried with the user until they loose their current session (when the cookie is deleted) or the server code destroys the $_SESSION variable.
$_SESSION itself is an array, and cannot hold any other value than an array, but you can change what's in that array. It acts just like any other php array, and can hold any value- from a class to a null value.
Since sessions are identified by the cookie, all the session data is lost when the cookie is deleted or it expires, since there is no client that has that cookie anymore. So, logically, the easiest way to "erase" a session would be to delete the cookie. While it does do what you want it to, it doesn't delete the data on the server. The way I've done it is this:
[php] $_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();[/php]
[highlight]REMEMBER- if the page uses sessions at all, you have to put session_start(); at the top. Even if the part is inside of an if() statement, you have to put it up there.[/highlight]
[/release]
rewrote it, because I was bored. If it needs fixing, let me now (make sure to snip out the releases!).
Has anybody used MyBB's plugin system before? And have they also used it's hook system to manipulate the postbit? I've seem to run into a roadblock with a little project I'm working on.
[QUOTE=Ac!dL3ak;30621854]
In php, you can access the cookies with the $_COOKIE array. Each cookie is included in the array, as the name as the key and the value as, well, the value. You can set cookies in php with the [highlight]set_cookie();[/highlight] function, and the simplest way to remove a cookie is setting it's expiration date in the past like this:
[php]<?php setcookie("PHPSESSID", "", time()-3600); ?> [/php]
[/QUOTE]
setcookie();
Also, as far as the example goes, you might not want to use PHPSESSID because the proper way to close a session is using session_destroy();.
[editline]21st June 2011[/editline]
[QUOTE=:awesome:;30622380]Has anybody used MyBB's plugin system before? And have they also used it's hook system to manipulate the postbit? I've seem to run into a roadblock with a little project I'm working on.[/QUOTE]
Haven't had too much expierence with MyBB's plugins, but it looks like there is a hook for "parse_message(_start/_end)", would that suit?
[QUOTE=deadeye536;30622944]setcookie();
Also, as far as the example goes, you might not want to use PHPSESSID because the proper way to close a session is using session_destroy();.[/QUOTE]
heh, I pulled the image out of my dropbox so it's highlighted because I used it before
[QUOTE=vepa;30620985]1. Just remove the while loop completely. Use mysql_fetch_assoc to set $row as you already are doing and set the $fwhatever variables from $row.[/QUOTE]
Why the fuck isn't this working.
[php]
function getinfo(){
$query = mysql_query("SELECT * FROM users WHERE id='$guid'") or die(mysql_error());
$row = mysql_fetch_assoc($query);
return $username = $row['username'];
}
[/php]
[php]
echo getinfo();
[/php]
because you should use pdo
[editline]21st June 2011[/editline]
and just use return $row['username'];
but first use pdo
It works when it's in the same file, but not through the require function.
did you connect to the database
and what's pdo? and yes
[QUOTE=alphaspida;30623426]and what's pdo? and yes[/QUOTE][url=http://www.google.com/search?q=pdo]you can't be serious[/url]
[QUOTE=TehWhale;30623443][url=http://www.google.com/search?q=pdo]you can't be serious[/url][/QUOTE]
google added the amount of times you've visited a page to the results :tinfoil:
first result:
[quote] You've visited this page 4 times. Last visit: 6/8/11[/quote]
[editline]21st June 2011[/editline]
I can make a guide if it's needed v:v:v
fuck it
[php]
if(isset($_COOKIE['uid'])){
$guid = $_COOKIE['uid'];
$userquery = mysql_query("SELECT * FROM users WHERE id='$guid'") or die(mysql_error());
$row = mysql_fetch_assoc($userquery);
$gusername = $row['username'];
$gpassword = $row['password'];
$gemail = $row['email'];
}
[/php]
[editline]22nd June 2011[/editline]
I'll fuck with sessions later, I've wasted enough time on this
[QUOTE=alphaspida;30623887]fuck it
[php]
$pdo = new PDO("mysql:dbname=database",$username,$password);
if(isset($_COOKIE['uid'])){
$guid = $_COOKIE['uid'];
$prepare = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$prepare->execute((int)$guid);
$row = $prepare->fetch(PDO::FETCH_ASSOC);
$gusername = $row['username'];
$gpassword = $row['password'];
$gemail = $row['email'];
}
[/php][/QUOTE]
:eng101:
Confusing. Is PDO like a hidden class or some shit?
[QUOTE=alphaspida;30624128]Confusing. Is PDO like a hidden class or some shit?[/QUOTE]That's like asking if mysql_* are hidden functions
[QUOTE=alphaspida;30624128]Confusing. Is PDO like a hidden class or some shit?[/QUOTE]
[url]http://php.net/manual/en/book.pdo.php[/url]
it's a built-in class for php
Just curious, because as a beginner moving onto OOP in PHP I saw $something = new Something to create an instance of a class and -> to reference functions inside.
[editline]22nd June 2011[/editline]
[QUOTE=Ac!dL3ak;30623992]
[php]
$pdo = new PDO("mysql:dbname=database",$username,$password);
if(isset($_COOKIE['uid'])){
$guid = $_COOKIE['uid'];
$prepare = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$prepare->execute((int)$guid);
$row = $prepare->fetch(PDO::FETCH_ASSOC);
$gusername = $row['username'];
$gpassword = $row['password'];
$gemail = $row['email'];
}
[/php]
:eng101:[/QUOTE]
[php]
$pdo = new PDO("mysql:dbname=database",$username,$password);
if(isset($_COOKIE['uid'])){
$guid = $_COOKIE['uid'];
$prepare = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$prepare->execute((array)$guid);
$row = $prepare->fetch(PDO::FETCH_ASSOC);
$gusername = $row['username'];
$gpassword = $row['password'];
$gemail = $row['email'];
}
[/php]
:eng101:
Thanks for the help so far.
[QUOTE=alphaspida;30624163]Just curious, because as a beginner moving onto OOP in PHP I saw $something = new Something to create an instance of a class and -> to reference functions inside.
[editline]22nd June 2011[/editline]
[php]
$pdo = new PDO("mysql:dbname=database",$username,$password);
if(isset($_COOKIE['uid'])){
$guid = $_COOKIE['uid'];
$prepare = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$prepare->execute(array((int)$guid));
$row = $prepare->fetch(PDO::FETCH_ASSOC);
$gusername = $row['username'];
$gpassword = $row['password'];
$gemail = $row['email'];
}
[/php]
:eng101:
Thanks for the help so far.[/QUOTE]
:eng101:
sorry I messed it up the first time
How do you embed things like {forum:hello} inside html (it seems)? I don't know what it's called but I see it used in forum templates
They probably just read the the template in and replace certain phrases with there real counterpart, then display it.
I don't understand
[QUOTE=alphaspida;30640561]I don't understand[/QUOTE]
It basically does a search and replace for {text inside}. On each instance that it finds it calls a function that returns the variable or whatever goes inside the brackets.
So let's say you have {forum:hello}. Well the regular expression will find the brackets with stuff inside and then send "forum:hello" to a "function interpret_shit($shit)".
This function will then replace it with the appropriate variable or whatever. In this case it's probably the contents of $forum->hello.
How do I make this look fancy & shit? [url]http://mmavipc.dyndns.org:8080/index.php[/url]
use a stylesheet and close your body and html tags, as well as setting encoding and doctype??
[QUOTE=Erp;30641199]use a stylesheet and close your body and html tags, as well as setting encoding and doctype??[/QUOTE]
oh yeah, forgot about the body and html tags derp
I really want to use PDO but i'm starting to hate class-based stuff because of the 200% extra code needed for everything, and it looks ugly in-code.
Sorry, you need to Log In to post a reply to this thread.