To achieve the Avatar upload function for multiple users PHP code is suitable for landing page production _php instance

Source: Internet
Author: User
Tags button type rand

A web site, in fact, is a few specific features of a combination, and the replacement of the user avatar is in these functions. Today to do a test, for different users, to achieve Avatar upload function.

--------------------------------------------------------------------------------

Finished picture

Ideas
• For different users to upload avatar, we want for each logged in user to create a folder, the name of the folder is the current user name.

• After the user uploaded successfully, jump to the user login after the successful page, and refresh the user avatar.

Landing page

Form making

 <form role= "form" action= "/forindex.php" > <div class= "Form-group" > <label for= "Name" > Username </label> <input type= "text" class= "Form-control" id= "username" name= "username" placeholder = "Please enter name" > </div> <div class= "Form-group" > <label for= "inputfile" > File input </label> <input ty pe= "Password" id= "inputfile" name= "password" > <p "class=" > here is an example of block-level help text. </p> </div> <div class= "Form-group" > <label> Please enter the code </label> <input type= "text" id= "ch Eckcode "name=" Checkcode "/>  "/><a href=" javascript:void (0); "onclick=" Change () "> Can't see </a> </div> <script> function 
 Change () {document.getElementById ("Imagecheckcode"). src = "./store.php?r=" + math.random (); } </script> <button type= "Submit" class= "BTN Btn-default" > Submit </button> </form> 

Verify code creation

<?php session_start ()//must be declared at the very beginning of PHP to open session//Use GD Imagecreatetruecolor (); Create a background image $image =

Imagecreatetruecolor (100,40);
Generate Fill Color $bgcolor = imagecolorallocate ($image, 255,255,255);

Fills the fill color onto the background image Imagefill ($image, 0,0, $bgcolor);
Generates a random 4-digit letter and a digital hybrid 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));
 In order to avoid the user difficult to identify, removed some ambiguous letters and numbers $rawstr = ' abcdefghjkmnopqrstuvwxyz23456789 ';
 $fontcontent = substr ($rawstr, Rand (0,strlen ($RAWSTR)), 1);
 Stitching is about to be born in the verification code $checkcode. = $fontcontent;
 Avoid the resulting picture overlap $x + 20;
 $y = rand (10,20); 
Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}//Save to Session variable $_session[' Checkcode ']= $checkcode; Generates 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 some noise lines here are 4 for ($i =0; $i <4; $i + +) {//set to Light-colored line to prevent the noisy bingoMain $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);

 Releasing resources, destroying the 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>

Validate page

As the most core of this test is the replacement of the user's head, so the user name for the time being, whichever is the root.

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, landing success!" "." <br/>3 seconds will automatically jump to the personal 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, to first implement the page jump, there are many ways. This article uses a way to increase header information, and here are a few small instances of page jumps.

Header function

< PHP 
//Redirect Browser
header ("Location:http://blog.csdn.net/marksinoberg"); 
Ensure that after redirection, subsequent code will not be executed 
exit;
? >

Note: Location and: cannot have spaces between

META tags

< meta HTTP-EQUIV = "refresh" content = "1;url=http://blog.csdn.net/marksinoberg" >

Note: content can be controlled to complete the jump within a few seconds.

Javascript

< php 
$ url = "Http://bbs.lampbrother.net"; 
echo "< script language = ' javascript ' 
type = ' text/javascript ' > '; 
echo "window.location.href = ' $url '"; 
echo "</script >"; 
? >

Note: using JavaScript, the location of the code can be arbitrary, as long as it meets the requirements of the grammar.

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 uploaded to the server, and then PHP use Move_uploaded_file to achieve 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 '];
The name that is spliced into the file on the server
$server _name = $path. $username. ". PNG ";


if ($_files[' photo '] [' Error ']>0 ') {
 die ("Wrong!") ". $_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 the personal homepage! "; 
 Header ("refresh:3;url=./personalpage.php");
} else{
 //echo "<BR>". Upload failed! ". $_files[' photo ' [' Error ']; 
 echo "Sorry, upload avatar failed!" ";
 Header ("refresh:2;url=./index.php");
>

Final results

Landing page

Validation results

Personal homepage

Latest Avatar

Summarize

Review the harvest of this experiment.
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 making and use
javascript:void (0); using the core

The general content is so much, although did not increase the beautification effect, but though small, viscera is also regarded as the taste.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.