For multiple users, the Avatar upload function is implemented. PHP Code is applicable to login page creation and Avatar page creation.
To put it bluntly, a website is a combination of several specific functions, and changing user portraits is included in these functions. Let's take a test today to implement the Avatar upload function for different users.
--------------------------------------------------------------------------------
Finished Diagram
Ideas
• For different users to upload portraits, we need to create a folder for each logged-on user. The folder name is subject to the User Name of the current user.
• After the user successfully uploads the image, go to the user logon page and refresh the user profile.
Login Page
Form creation
<Form role = "form" action = ". /forindex. php "> <div class =" form-group "> <label for =" name "> User name </label> <input type =" text "class =" form-control" id = "username" name = "username" placeholder = "enter a name"> </div> <div class = "form-group"> <label for = "inputfile"> file input </label> <input type = "password" id = "inputfile" name = "password"> <p class = "help-block"> block-level help text. </P> </div> <div class = "form-group"> <label> enter the Verification Code </label> <input type = "text" id = "checkcode" name = "checkcode"/> "/> <A href =" javascript: void (0); "onclick =" change () "> cannot see </a> </div> <script> function change () {document. getElementById ("imagecheckcode "). src = ". /store. php? R = "+ Math. random () ;}</script> <button type =" submit "class =" btn-default "> submit </button> </form>
Verification code creation
<? Phpsession_start (); // It must be declared at the beginning of php to enable session // use imagecreatetruecolor () of gd; create a background image $ image = imagecreatetruecolor (, 40 ); // generate the fill color $ bgcolor = imagecolorallocate ($ image, 255,255,255); // fill the fill color to the background image imagefill ($ image, $ bgcolor ); /////// generate a random 4-digit verification code with a mix of letters and numbers $ checkcode = ''; for ($ I = 0; $ I <4; $ I ++) {$ fontsize = rand (6, 8); $ fontcolor = imagecolorallocate ($ image, rand (0,255), rand (0,255), rand (0,255 )); // some ambiguous letters and numbers are removed to avoid user identification. $ rawstr = 'abcdefghjkmnopqrstuvwxyz23456789 '; $ fontcontent = substr ($ rawstr, rand (0, strlen ($ rawstr), 1); // concatenate the forthcoming Verification Code $ checkcode. = $ fontcontent; // avoid overlapping images generated $ x + = 20; $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // save it to the session variable $ _ SESSION ['checkcode'] = $ checkcode; // generate interference points, here are 200 for ($ I = 0; $ I <200; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,255), rand (50,255 ), rand (50,255); imagesetpixel ($ image, rand (0,100), rand (), $ pointcolor );} // generate some interference lines. Here there are 4 for ($ I = 0; $ I <4; $ I ++) {// set the line to a light color, prevents leeching. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand ), rand (), $ linecolor);} header ('content-type: image/png '); imagepng ($ image ); // release the resource and destroy the execution object imagedestroy ($ image );
JavaScript refresh Verification Code
<A href = "javascript: void (0);" onclick = "change ()"> invisible </a> <script> function change () {document. getElementById ("imagecheckcode "). src = ". /store. php? R = "+ Math. random () ;}</script>
Verification page
Because the most important part of this test is the change of the user's profile picture, we do not care about the user name for the time being. Use Root as the standard.
Verification Logic
<? Php session_start (); header ("Content-Type: text/html; charset = UTF-8"); $ username = $ _ REQUEST ['username']; $ password = $ _ REQUEST ['Password']; if (strtolower ($ _ REQUEST ['checkcode'] = $ _ SESSION ['checkcode']) {if (! Is_dir ($ username) {mkdir ($ username);} echo "Congratulations! login successful! "." <Br/> the personal homepage is automatically displayed 3 seconds later! "; $ _ SESSION ['username'] = $ username; header (" refresh: 3; url =. /personalpage. php ");} else {echo" sorry, login failed! "; Header (" refresh: 3; url =. /index. php "); // echo" <script> window. location. href = '. /index. php' </script> ";}
Page Jump
In PHP, there are many ways to jump pages first. This article uses the method of adding header information. The following describes several small page Jump instances.
Header Function
<? Php // redirect the browser header ("Location: http://blog.csdn.net/marksinoberg"); // make sure the subsequent code is not executed after the redirection;?>
Note:No space is allowed between Location and :.
Meta tag
<Meta http-equiv = "refresh" content = "1; url = http://blog.csdn.net/marksinoberg">
Note:Content can be redirected within several seconds.
JavaScript
< ?php $ url = "http://bbs.lampbrother.net" ; echo " < script language = 'javascript' type = 'text/javascript' > "; echo " window.location.href = '$url' "; echo " < /script > "; ?>
Note:When using JavaScript, the code can be stored anywhere, as long as it meets the syntax requirements.
Upload page
Personal Homepage
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Upload Core
The core of the upload is a form. We upload the image to the server, and then php uses move_uploaded_file to migrate the file and upload it.
<? Php session_start (); header ("Content-Type: text/html; charset = UTF-8"); // The storage location of the attachment and the attachment name $ path = ". /root/"; $ username = $ _ SESSION ['username']; // concatenate the file into the name of the file on the server $ server_name = $ path. $ username. ". png "; if ($ _ FILES ['photo'] ['error']> 0) {die (" error! ". $ _ FILES ['photo '] ['error']);} if (move_uploaded_file ($ _ FILES ['photo'] ['tmp _ name'], $ server_name )) {// echo "<BR> ". "Upload Success! "; Echo" congratulations, upload successful! "." <Br/> the personal homepage is automatically displayed 3 seconds later! "; Header (" refresh: 3; url =./personalpage. php ");} else {// echo" <BR> "." Upload Failed! ". $ _ FILES ['photo'] ['error']; echo" sorry, an error occurred while uploading the Avatar! "; Header (" refresh: 2; url =./index. php ") ;}?>
Final Result
Login Page
Verification Result
Personal Homepage
Latest profile picture
Summary
Review the results of this experiment.
• The session must be enabled at the beginning of the PHP file session_start ()
• Page Jump methods that php can achieve
• Upload files
• Create and use verification Codes
• JavaScript: void (0); Core
The general content is so much that although there is no improvement in the beautification effect, although the sparrow is small, the five dirty data is not enough.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.