A website, in fact, is a combination of a few specific features, and replace the user avatar is in these features. Today to do a test, for different users, to achieve the Avatar upload function.
Finished drawings
Ideas
For different users to upload avatars, we want to create a folder for each logged-in user, the name of the folder is the current user's user name prevail.
After the user uploads successfully, jump to the page after the user logs in successfully and refresh the user picture.
Landing page
Form making
<form role= "Form" action= "./forindex.php" > <p class= "Form-group" > <label for= "name" > User name </label > <input type= "text" class= "Form-control" id= "username" name= "username" placeholder= "Please enter name" > </ p> <p class= "Form-group" > <label for= "inputfile" > File input </label> <input type= "Password" I D= "inputfile" name= "password" > <p class= "Help-block" > here is an instance of block-level help text. </p> </p> <p class= "Form-group" > <label> Please enter the verification code </label> <input type= "Tex T "id=" Checkcode "name=" Checkcode "/> "/> <a href=" javascript:void (0); "onclick=" Change () "> Unclear </a> </p> <script> function Change () {document.getElementById ("Imagecheckcode"). src = "./store.php?r=" + math.random (); } </script> <button type= "Submit" class= "BTN Btn-defauLT "> Submit </button></form>
Verification Code Production
<?phpsession_start ();//must be declared at the beginning of PHP to open session//using GD's Imagecreatetruecolor (); Create a background map $image = Imagecreatetruecolor (100,40);//Generate fill Color $bgcolor = imagecolorallocate ($image, 255,255,255);//fills the fill color onto the background map Imagefill ($ image,0,0, $bgcolor);////////generates a random 4-bit letter and a digital mixed verification code $checkcode= '; for ($i =0; $i <4; $i + +) {$fontsize = rand (6,8); $fontcolor = Imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255)); To avoid the user's difficulty in identifying, some ambiguous letters and numbers are removed $rawstr = ' abcdefghjkmnopqrstuvwxyz23456789 '; $fontcontent = substr ($rawstr, Rand (0,strlen ($RAWSTR)), 1); Stitching the upcoming verification code $checkcode. = $fontcontent; Avoid overlapping of generated images $x + = 20; $y = rand (10,20); Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }//Save to SESSION variable $_session[' checkcode ']= $checkcode;//Generate some disturbing 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 (0,30), $pointcolor);} Generate a number of interference lines here are 4 for ($i =0; $i <4; $i + +) {//set toLight-coloured lines to prevent distracting $linecolor = Imagecolorallocate ($image, Rand (50,255), Rand (50,255), Rand (50,255)); Imageline ($image, Rand (0,99), Rand (0,29), Rand (0,99), Rand (0,29), $linecolor);} Header (' content-type:image/png '); Imagepng ($image);//release resources, destroy execution Object Imagedestroy ($image);
JavaScript Refresh Verification Code
<a href= "javascript:void (0); onclick=" Change () "> Unclear </a><script> function change () { document.getElementById ("Imagecheckcode"). src = "./store.php?r=" + math.random (); } </script>
Verification page
As the most core of this test is the replacement of the user's avatar, so the user name we do not care, the root of the subject.
Validation 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 on your successful landing!" "." <br/>3 seconds will automatically jump to your homepage! "; $_session[' username '] = $username; Header ("refresh:3;url=./personalpage.php"); } else{ echo "Sorry, the landing failed!" "; Header ("refresh:3;url=./index.php"); echo "<script>window.location.href="./index.php ' </script> "; }
Page Jump
In PHP, there are many ways to implement a page jump first. This article uses the way to add header information, and here are a few small examples of page jumps.
Header function
< php//Redirect Browser header ("location:http://www.php.cn/");//Ensure that subsequent code will not be executed after redirection; >
Note: There can be no spaces between location and:
META tags
< meta http-equiv = "Refresh" content = "1;url=http://www.php.cn/" >
Note: Content can be controlled to complete jumps within seconds.
Javascript
< php $ url = "Http://bbs.lampbrother.net"; echo "< script language = ' javascript ' type = ' Text/javascript ' >"; echo "window.location.href = ' $url '"; echo "< /script >"; ? >
Note: With JavaScript, you can place your code anywhere you want, as long as you are in line with the syntax.
Upload page
Personal homepage
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "
Upload Core
The core of the upload is still a form, we upload the image uploaded to the server, and then PHP using Move_uploaded_file to implement the file migration, upload.
<?php session_start (); Header ("Content-type:text/html;charset=utf-8");//The storage location of the attachment, the name of the attachment $path = "./root/"; $username = $_session[' username ') ;//stitching 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 success!" "." <br/>3 seconds will automatically jump to your homepage! "; Header ("refresh:3;url=./personalpage.php");} else{ //echo "<BR>". " Upload failed! ". $_files[' photo ' [' Error ']; echo "Sorry, uploading the avatar failed!" "; Header ("refresh:2;url=./index.php");}? >
Final result
Landing page
Validation results
Personal homepage
Latest Avatar
Summarize
Look back at the harvest of this experiment.
The session must be opened at the beginning of the PHP file Session_Start ()
PHP can be implemented by the way the page jumps
Uploading files
Verification code production and use
Javascript:void (0); The use of the core
The general content is so much, although did not increase the beautification effect, but the very little, five zang also is a taste.