Web Development Questions That Don't Need Their Own Thread v2
3,079 replies, posted
[QUOTE=PieClock;30280119]What's the best method of generating 6-7 random letters and numbers for a page "id", while making sure the same one never appears twice? (php)[/QUOTE]
[php]
<?php
substr(md5(time().rand(0,100)),0,7);
?>
[/php]
[QUOTE=pdkm931;30280220]sha1(time()) or md5(time())?[/QUOTE]
I feel pretty stupid now. Thanks.
[QUOTE=PieClock;30280260]I feel pretty stupid now. Thanks.[/QUOTE]
the sha1 returns 40 characters and the md5 returns 32; I suggest using substr or some other method to shorten it.
In windows command line, why wont it let me run "cake bake", it says that php is not recognized as an internal or extenal command. It worked yesterday, and yes I have set the environmental variables:
"C:\xampp\cake\cake\console\; C:\xampp\php"
[QUOTE=Ac!dL3ak;30280319]the sha1 returns 40 characters and the md5 returns 32; I suggest using substr or some other method to shorten it.[/QUOTE]
Yeah, I know.
PHP's [URL="http://php.net/manual/en/function.uniqid.php"]uniqid[/URL] function + md5(if you really want a string) is probably a good pick. No need to faff about with rand() and time(), and less likely to cause collisions.
Whats the best/most user friendly way to embed an html file into an html file?
Just using iframes?
[QUOTE=Richy19;30283082]Whats the best/most user friendly way to embed an html file into an html file?
Just using iframes?[/QUOTE]
If you're okay with having this embedding done on the server, and your server supports it (apache - read: server-side includes), I believe the syntax is:
[code]<!--#include virtual="path/to/file.html" -->[/code]But I'd be a bit wary of using this method since it's very platform and setup specific. Off the top of my head, iframes are probably your best bet, but it really depends on exactly what you're working with.
I'M SO RICH
[IMG]http://i.imgur.com/MPSqF.png[/IMG]
I'm having trouble with session for php using login and looking around website. I've tried google, but I don't really get how session works. What is $_Session['XYZ'] and how is it used.
[QUOTE=zzlawlzz;30290083]I'm having trouble with session for php using login and looking around website. I've tried google, but I don't really get how session works. What is $_Session['XYZ'] and how is it used.[/QUOTE]session_start();
if($_SESSION['Auth'] = 1):
$Username = $_SESSION['Username'];
// user is logged in whatever
endif;
[editline]6th June 2011[/editline]
Those are just variables used throughout the session. You must use session_start(); to use them
[QUOTE=zzlawlzz;30290083]I'm having trouble with session for php using login and looking around website. I've tried google, but I don't really get how session works. What is $_Session['XYZ'] and how is it used.[/QUOTE]
whenever you use sessions, you have to put
[php]<?php session_start(); ?>[/php]
at the beginning of the page. What basically happens is php gives the client a cookie, which identifies that client. That cookie corresponds to a file on the server that contains the data for the session.
tl;dr: 1) use session_start(); at the top of the page
2) $_SESSION can contain any value
3) it differs from client to client
[QUOTE=TehWhale;30290162]session_start();
if($_SESSION['Auth'] = 1):
$Username = $_SESSION['Username'];
// user is logged in whatever
endif;
[editline]6th June 2011[/editline]
Those are just variables used throughout the session. You must use session_start(); to use them[/QUOTE]
How would Auth and Username be defined?
[url]http://www.w3schools.com/php/php_sessions.asp[/url]
[editline]6th June 2011[/editline]
[QUOTE=zzlawlzz;30290252]How would Auth and Username be defined?[/QUOTE]
just like any other variable
[QUOTE=zzlawlzz;30290252]How would Auth and Username be defined?[/QUOTE]session_start();
$_SESSION['Auth'] = 1;
[QUOTE=TehWhale;30290162]session_start();
if($_SESSION['Auth'] == 1):
$Username = $_SESSION['Username'];
// user is logged in whatever
endif;[/QUOTE]
personally, I think they're much better than cookies but that's just me.
Just like any other variable.
[editline]6th June 2011[/editline]
fucking automerge man
[QUOTE=TehWhale;30290278]Just like any other variable.
[editline]6th June 2011[/editline]
fucking automerge man[/QUOTE]
you're telling me!
[QUOTE=Ac!dL3ak;30290270]personally, I think they're much better than cookies but that's just me.[/QUOTE]Then the user has to login every time they visit the site. Combine session with cookies and you have a winner
[editline]6th June 2011[/editline]
fuck really man
I'm guessing every time that person gets off where the session is at and into another part where there isn't any session_start that person will have to re-log again?
[editline]6th June 2011[/editline]
When they enter a page that requires for that person to be logged
[QUOTE=zzlawlzz;30290361]I'm guessing every time that person gets off where the session is at and into another part where there isn't any session_start that person will have to re-log again?[/QUOTE]Sessions exists until they close the browser (or tab, not 100% sure).
[QUOTE=TehWhale;30290313]Then the user has to login every time they visit the site. Combine session with cookies and you have a winner
[editline]6th June 2011[/editline]
fuck really man[/QUOTE]
[php]session_set_cookie_params(time()+time());[/php]
[editline]6th June 2011[/editline]
[QUOTE=TehWhale;30290375]Sessions exists until they close the browser (or tab, not 100% sure).[/QUOTE]
it's a cookie man!
[QUOTE=zzlawlzz;30290361]I'm guessing every time that person gets off where the session is at and into another part where there isn't any session_start that person will have to re-log again?
[editline]6th June 2011[/editline]
When they enter a page that requires for that person to be logged[/QUOTE]You can't use anything session related unless you use session_start() first
[QUOTE=TehWhale;30290387]You can't use anything session related unless you use session_start() first[/QUOTE]
oh ya, that's what I meant.
I may think I get it now...
[editline]6th June 2011[/editline]
If index have that $_session['Auth'] == 1 and $username then you goto 2nd page, will it be still there if I want to see if auth is still 1 and username is still the same?
Let me explain how cookies work.
[RELEASE][H2]How Cookies Work[/H2]
When a website wants to identify a client, they normally do so using a cookie. This cookie distinguishes that client from all the other clients, because it contains data normally specific to that client. The cookie is downloaded to the client via HTTP headers, which is stored on the client's computer.
[img]http://dl.dropbox.com/u/11275736/sessid.png[/img]
These cookies have four main parts to it:
[list]
[*] the name (as hightlighted above),
[*] the data,
[*] the domain and path it's for,
[*] and when it expires.
[/list]
these are controlled by the server. The data is normally short (like I said- to identify clients), and the expiration date is normally when the browser closes. Using php, you can modify cookie's names, data, paths, and the expiration date using cookie functions. While the server is free to define these things, it's up to the client (and their browser) on whether or not to follow them through.
The client will always send the cookies back to the server as long as they aren't expired. So yes, it is page-to-page.
I hope that was helpful ;)[/RELEASE]
[QUOTE=Ac!dL3ak;30290270]personally, I think they're much better than cookies but that's just me.[/QUOTE]
Well, PHP sessions use a cookie with a unique session id (PHPSESSID). This cookie is sent along and used to identify the user and load his session data.
You can bypass the session_start() call by setting [B]session.auto_start = On[/B] in your php.ini file.
I wrote this very quick and simple session abstraction class for dirp, which supports namespacing: [URL]https://github.com/FilipeD/dirp/blob/master/dirp/session.php[/URL]
By namespacing I mean you can choose the top-level where your session data resides. So if you have two scripts that both try to set the session variable 'auth', they are able to work collision free simply by defining an individual namespace:
[php]
$session_one = session::factory('one');
$session_two = session::factory('two');
$session_one->set('auth', true);
echo $session_two->get('auth');
[/php]Output:
[code]null[/code][editline]6th June 2011[/editline]
[QUOTE=Ac!dL3ak;30290714]Let me explain how cookies work.
[H2]How Cookies Work[/H2][/QUOTE]
Close. The expiration date for cookies is defined by the server sending it, even though the client ultimately is free to do whatever he wants with them. They're also not sent as a text packet, but as part of the response's HTTP headers.
[QUOTE=StinkyJoe;30290734]stuff[/QUOTE]
shhh, you'll confuse him
[editline]6th June 2011[/editline]
updated, just for you ;)
[QUOTE=Ac!dL3ak;30290830]shhh, you'll confuse him
[editline]6th June 2011[/editline]
updated, just for you ;)[/QUOTE]
I think I get how cookies work, but it's session that is really confusing me.
Either I'm thinking too hard for something that isn't there but I think is there, or I'm just really bad at understand this.
[editline]6th June 2011[/editline]
would cookie be an easy way out if I don't get how to use session? or is there a bad side of cookies.
In windows command line, why wont it let me run "cake bake", it says that php is not recognized as an internal or extenal command. It worked yesterday, and yes I have set the environmental variables:
"C:\xampp\cake\cake\console\; C:\xampp\php"
[QUOTE=zzlawlzz;30291078]I think I get how cookies work, but it's session that is really confusing me.
Either I'm thinking too hard for something that isn't there but I think is there, or I'm just really bad at understand this.
[editline]6th June 2011[/editline]
would cookie be an easy way out if I don't get how to use session? or is there a bad side of cookies.[/QUOTE]
here, let me extend my previous attempt at explaining it:
[release][h2]Cookies, Sessions, PHP, and YOU![/h2]
When you create a session using session_start();, php (I'm assuming from this part) checks to see if the session cookie was sent with the headers. If not, it sends the session cookie to the client. A session cookie would look something like this:
[img]http://dl.dropbox.com/u/11275736/sesscook.png[/img]
That is a session cookie generated for my website. It contains the PHPSESSID, the unique identifier of the client.
On the server, you can store and dynamic data based on who the client is. Here are the first few lines of my admin page:
[img]http://dl.dropbox.com/u/11275736/first.png[/img]
it starts the session, checks to see if someone's logged in, and if needed, redirects them. The $_SESSION array is basically an array that is stored for the client on the server; all data types can be stored in it.
All sessions are are different array values based on who's connected.
[/release]
Sorry, you need to Log In to post a reply to this thread.